在android中,如何根据优先级对单行中的多个值进行排序,类似于Excel中的Sort函数?
Map<String,String,Float,Integer> shoes; // Style, Color, Price, Quantity
shoes.put("Boots","Red",15.50,5);
shoes.put("Boots","Green",17.50,2);
shoes.put("Skate","Red",12.25,6);
shoes.put("Skate","Blue",13.05,9);
shoes.Sort(1,0,2,3); // sorts by color, then by style
shoes.Sort(2,0,1,3); // sorts by price, then by style
答案 0 :(得分:0)
Kae提出的建议是正确的,但是,你可以这样做:
看一下这个帖子:Sort a Map<Key, Value> by values (Java)
但是,您应该考虑根据需要创建一个类。
[]中 内托
答案 1 :(得分:0)
有一个很好的示例代码块here,它涵盖了设置通用比较器。要对多个列进行排序将需要多次调用,但它将具有所需的效果。来自Collections spec
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
所以你从鞋子开始
public class Shoe
{
String style;
String color;
float price;//There are some reasons to not use float for money, google has lots of blogs etc
int quantity;
}
然后根据上面链接的教程制作比较器,最终:
ArrayList<Shoe> shoes = makeShoes();
Collections.sort(shoes,col1Comparator);
Collections.sort(shoes,col2Comparator);
Collections.sort(shoes,col3Comparator);