使用比较器进行自定义sorting

我想开发车列表的sorting演示。 我正在使用数据表来显示汽车列表。 现在其实我想按汽车颜色sorting。 这里不是按字母顺序sorting。 我想用我的自定义sorting顺序,如红色车先来,然后蓝色,等等。

为此,我尝试使用Java Comparator和可比较的function,但它只允许按字母顺序sorting。

那么,任何一个人都可以引导我去实施这个技术的使用,这样sorting就变得更快了。

import java.util.ArrayList; import java.util.Comparator; import java.util.List; public class CarSort implements Comparable<CarSort>{ String name; String color; public CarSort(String name, String color){ this.name = name; this.color = color; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } //Implement the natural order for this class public int compareTo(CarSort c) { return getName().compareTo(c.getName()); } static class ColorComparator implements Comparator<CarSort> { public int compare(CarSort c1, CarSort c2) { String a1 = c1.getColor(); String a2 = c2.getColor(); return a1.compareTo(a2); } } public static void main(String[] args) { List<CarSort> carList = new ArrayList<CarSort>(); List<String> sortOrder = new ArrayList<String>(); carList.add(new CarSort("Ford Figo","Silver")); carList.add(new CarSort("Santro","Blue")); carList.add(new CarSort("Honda Jazz","Magenta")); carList.add(new CarSort("Indigo V2","Red")); sortOrder.add("Red"); sortOrder.add("Magenta"); sortOrder.add("Blue"); sortOrder.add("Silver"); // Now here I am confuse how to implement my custom sort } } 

我build议你为你的汽车颜色创build一个枚举,而不是使用string,枚举的自然顺序将是你声明常量的顺序。

 public enum PaintColors { SILVER, BLUE, MAGENTA, RED } 

  static class ColorComparator implements Comparator<CarSort> { public int compare(CarSort c1, CarSort c2) { return c1.getColor().compareTo(c2.getColor()); } } 

您将string更改为PaintColor,然后在您的汽车列表中变成:

 carList.add(new CarSort("Ford Figo",PaintColor.SILVER)); ... Collections.sort(carList, new ColorComparator()); 

这个怎么样:

 List<String> definedOrder = // define your custom order Arrays.asList("Red", "Green", "Magenta", "Silver"); Comparator<Car> comparator = new Comparator<Car>(){ @Override public int compare(final Car o1, final Car o2){ // let your comparator look up your car's color in the custom order return Integer.valueOf( definedOrder.indexOf(o1.getColor())) .compareTo( Integer.valueOf( definedOrder.indexOf(o2.getColor()))); } }; 

原则上,我同意使用enum是更好的方法,但是这个版本更加灵活,因为它可以让你定义不同的sorting顺序。

更新

番石榴有这个function烘焙到其Ordering类:

 List<String> colorOrder = ImmutableList.of("red","green","blue","yellow"); final Ordering<String> colorOrdering = Ordering.explicit(colorOrder); Comparator<Car> comp = new Comparator<Car>() { @Override public int compare(Car o1, Car o2) { return colorOrdering.compare(o1.getColor(),o2.getColor()); } }; 

这个版本稍微冗长些。


再次更新

Java 8使比较器更加冗长:

 Comparator<Car> carComparator = Comparator.comparing( c -> definedOrder.indexOf(c.getColor())); 

比较排队

 List<Object> objList = findObj(name); Collections.sort(objList, new Comparator<Object>() { @Override public int compare(Object a1, Object a2) { return a1.getType().compareToIgnoreCase(a2.getType()); } }); 

我认为这可以做到如下:

  class ColorComparator implements Comparator<CarSort> { private List<String> sortOrder; public ColorComparator (List<String> sortOrder){ this.sortOrder = sortOrder; } public int compare(CarSort c1, CarSort c2) { String a1 = c1.getColor(); String a2 = c2.getColor(); return sortOrder.indexOf(a1) - sortOrder.indexOf(a2); } } 

为了sorting使用这个:

  Collections.sort(carList, new ColorComparator(sortOrder)); 

PS我写了这个代码在一个浏览器不debugging它。 但是这个概念应该是清楚的。

我不得不做类似于肖恩和伊莱克斯的答案。
但是我有太多的选项来显式地定义sorting顺序,并且只需要将某些条目浮动到列表的前面…以指定的(非自然的)顺序。
希望这对别人有帮助。

 public class CarComparator implements Comparator<Car> { //sort these items in this order to the front of the list private static List<String> ORDER = Arrays.asList("dd", "aa", "cc", "bb"); public int compare(final Car o1, final Car o2) { int result = 0; int o1Index = ORDER.indexOf(o1.getName()); int o2Index = ORDER.indexOf(o2.getName()); //if neither are found in the order list, then do natural sort //if only one is found in the order list, float it above the other //if both are found in the order list, then do the index compare if (o1Index < 0 && o2Index < 0) result = o1.getName().compareTo(o2.getName()); else if (o1Index < 0) result = 1; else if (o2Index < 0) result = -1; else result = o1Index - o2Index; return result; } //Testing output: dd,aa,aa,cc,bb,bb,bb,a,aaa,ac,ac,ba,bd,ca,cb,cb,cd,da,db,dc,zz } 

在Java 8中,你可以这样做:

你首先需要一个枚举:

 public enum Color { BLUE, YELLOW, RED } 

汽车类:

 public class Car { Color color; .... public Color getColor() { return color; } public void setColor(Color color) { this.color = color; } } 

然后,使用你的车名单,你可以简单地做:

 Collections.sort(carList, Comparator:comparing(CarSort::getColor)); 

定义一个枚举types为

 public enum Colors { BLUE, SILVER, MAGENTA, RED } 

color数据types从String更改为Colors将getter的返回types和参数types以及color的setter方法更改为Colors

定义比较器types如下

 static class ColorComparator implements Comparator<CarSort> { public int compare(CarSort c1, CarSort c2) { return c1.getColor().compareTo(c2.getColor()); } } 

在向List添加元素之后,通过传递List和Comparator对象作为参数来调用Collection的sorting方法

Collections.sort(carList, new ColorComparator()); 然后使用ListIterator打印。

全class实施如下:

 package test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.ListIterator; public class CarSort implements Comparable<CarSort>{ String name; Colors color; public CarSort(String name, Colors color){ this.name = name; this.color = color; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Colors getColor() { return color; } public void setColor(Colors color) { this.color = color; } //Implement the natural order for this class public int compareTo(CarSort c) { return getName().compareTo(c.getName()); } static class ColorComparator implements Comparator<CarSort> { public int compare(CarSort c1, CarSort c2) { return c1.getColor().compareTo(c2.getColor()); } } public enum Colors { BLUE, SILVER, MAGENTA, RED } public static void main(String[] args) { List<CarSort> carList = new ArrayList<CarSort>(); List<String> sortOrder = new ArrayList<String>(); carList.add(new CarSort("Ford Figo",Colors.SILVER)); carList.add(new CarSort("Santro",Colors.BLUE)); carList.add(new CarSort("Honda Jazz",Colors.MAGENTA)); carList.add(new CarSort("Indigo V2",Colors.RED)); Collections.sort(carList, new ColorComparator()); ListIterator<CarSort> itr=carList.listIterator(); while (itr.hasNext()) { CarSort carSort = (CarSort) itr.next(); System.out.println("Car colors: "+carSort.getColor()); } } }