Android 对多维对象或 int 数组进行排序
有没有办法对多维对象进行排序?
Object[][] datetable= new Object[2][2];
datetable[0][0] = 2;
datetable[0][1] = "Name2";
datetable[1][0] = 1;
datetable[1][1] = "Name1";
如果没有,我将如何对多维 int 数组进行排序?
int[][] numtable= new int[2][2];
numtable[0][0] = 2;
numtable[0][1] = 20;
numtable[1][0] = 1;
numtable[1][1] = 10;
我想使用第一组数字(numbtable[x] 或 datetable[x])对其进行排序 我可以得到一个多维字符串来排序没有问题,但这些我有麻烦。
Is there a way to sort a multidimensional Object?
Object[][] datetable= new Object[2][2];
datetable[0][0] = 2;
datetable[0][1] = "Name2";
datetable[1][0] = 1;
datetable[1][1] = "Name1";
If not, how would I sort a multidimensional int array?
int[][] numtable= new int[2][2];
numtable[0][0] = 2;
numtable[0][1] = 20;
numtable[1][0] = 1;
numtable[1][1] = 10;
I would like to sort it using the first set of numbers (numbtable[x] or datetable[x])
I can get a multidimensional string to sort no problem, but these I have trouble with.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对 int 数组进行排序是通过使用 Integer 对象并使用比较器来完成的,类似于 hotveryspicy 的答案,如下所示:
但是,从您的评论来看,您似乎表明您将有一个 5x12 的对象数组,其中散布着字符串和整数,但不彼此不相关(排序失去了项目之间的任何联系,这就是我这样认为的原因),您似乎将其合并到一个多维数组而不是单独的列表中(这就是我可能会去的方式)。
如果您确实转换为整数/字符串列表,则使用以下命令对它们进行排序,在本例中,我显示 int 版本:
Sorting arrays of int's is done through using Integer objects and using a comparator, similar to hotveryspicy's answer, as here:
However, from your comments, you seem to indicate you'll have a 5x12 array of objects interspersed with strings and ints which don't relate to each other (the sorting loses any connection between your items, which is what makes me think this), which you seem to be merging into one multidimensional array instead of separate lists (which is how i'd probably go).
if you do convert to a List of ints/strings, then use the following to sort them, in this case, i show the int version: