我有三个数组
String[] persons = {"jack","james","hill","catnis","alphonso","aruba"};
int[] points = {1,1,2,3,4,5};
int[] money = {25,66,24,20,21,22};
所有三个数组中的第n个位置属于同一个实体,例如:-
人[0]==分[0]==钱[0],即杰克有1分和25美元。
我想建立一个名单,排序的人按字母顺序(升序),如果开始字母是相同的,那么它应该检查点(降序),如果这些是相同的,那么它必须检查钱(降序)。
排序后的最终列表应该是{aruba,alphonso,catnis,hill,james,jack}。
4条答案
按热度按时间xytpbqjk1#
如果你想干些又快又脏的事:
前3行使用lambda根据数组索引定义比较器。
以下行使用流:
创建从0到persons.length-1的索引的int流
基于比较器序列对流的索引进行排序
将排序索引Map到人名
将其收集到列表中
lambda和streams不是很酷吗?
qfe3c7zg2#
所以我想你想要这样的东西:
然后创建一个
List<Person>
使用您拥有的数据(例如。,new Person("jack", 1, 25)
). 然后对它们进行排序:那会给你一个好印象
List<Person>
根据你的标准。aelbi1ox3#
与Evam的答案类似,您应该将这三个数据分组到一个类中。
然后你可以这样分类:
7rtdyuoh4#
如果你能有一个
Person
型号:然后你可以这样做:
结果:
persons=[person{name=jack,points=1,money=25},person{name=james,points=1,money=66},person{name=hill,points=2,money=24},person{name=catnis,points=3,money=20},person{name=alphonso,points=4,money=21},person{name=aruba,points=5,money=22}]
person[0]=person{name=jack,points=1,money=25}
persons=[person{name=aruba,points=5,money=22},person{name=alphonso,points=4,money=21},person{name=catnis,points=3,money=20},person{name=hill,points=2,money=24},person{name=james,points=1,money=66},person{name=jack,points=1,money=25}]
更紧凑的
sort
(但效率稍低,因为您必须提前运行所有比较):