假设我有一个长度为n的2D数组,是否有一种方法可以根据子数组的大小将2D数组按降序排序
例如:
{{"1","b","g"},{"e","3"},{"r","2","9","a"},{"2"}}
将成为:
{{"r","2","9","a"},{"1","b","g"},{"e","3"},{"2"}}
String tab[][] = new String[][]{{"1","b","g"},{"e","3"},{"r","2","9","a"},{"2"}};
String temp[];
for (int i = 0; i <tab.length; i++) {
for (int j = i+1; j <tab.length; j++) {
if(tab[i].length < tab[j].length) {
temp = tab[i];
tab[i] = tab[j];
tab[j] = temp;
}
}
}
System.out.println(Arrays.deepToString(tab));
输出:[[r, 2, 9, a], [1, b, g], [e, 3], [2]]
这个问题需要更多的细节。
考虑到给定的数据结构是一个数组,它看起来像-
[["1","b","g"],["e","3"],["r","2","9","a"],["2"]]
要在python中对这个数组排序,请考虑下面给出的代码:-
a = [["1","b","g"],["e","3"],["r","2","9","a"],["2"]]
a.sort(key=len)
将根据子数组的长度对数组进行排序