2016-11-07 90 views
0

我有有“身份证”和“流派”,这将是random.The样品JSON数组的巨大量JsonArray如下:如何基于java中的多个值对JsonArray进行排序?

{ 
    "category": [ 
     { "Id":"23" , "Genre":"English News" }, 
     { "Id":"43" , "Genre":"Entertainment" }, 
     { "Id":"255" , "Genre":"Music" }, 
     { "Id":"553" , "Genre":"Games" }, 
     { "Id":"97" , "Genre":"regional" } 
    ] 
} 

我需要这个jsonarray获得基于对排序:

(1.音乐 2.游戏 3.区域 4.英语新闻 5.娱乐)这个命令。

如何让这个json数组排序?有没有第三方的lib使用?

任何帮助,将不胜感激:)

回答

0

您可以创建多个比较和排序时可以用比较的任何。

public class ComparatorsGroup { 
       public static Comparator<Item> IdComparator = new Comparator<Item>() { 
        public int compare(Item item1, Item item2) { 
        return item1.getId().compareTo(item2.getId()); 
        } 
      }; 

      public static Comparator<Item> GenreComparator = new Comparator<Item>() { 
        public int compare(Item item1, Item item2) { 
        return item1.getGenre().compareTo(item2.getGenre()); 
        } 
      }; 
} 

在调用排序方法时,您可以根据所需排序传递任何比较器。

Collections.sort(myList, ComparatorsGroup.GenreComparator); 

欲了解更多详情,你可以看看由MkYong下面的例子。

Java object sorting example (Comparable and Comparator)

0

你将不得不写多个比较这里即您要排序上的每个字段比较,然后链,你想有一个序列的比较。将这些比较器列表传递给基本分区,在比较方法中,您将循环这些比较器并调用它们的比较方法。类似下面:

[Pseduo代码]

public class BaseComparator implements 
      Comparator<Map<String, String>> { 

     public List<Comparator<Map<String, String>>> listComparators; 

     public BaseComparator(
       List<Comparator<Map<String, String>>> comparators) { 
      this.listComparators = comparators; 
     } 

     @Override 
     public int compare(Map<String, String> obj1, Map<String, String> obj2) { 
      for (Comparator<Map<String, String>> comparator : listComparators) { 
       int result = comparator.compare(obj1, obj2); 
       if (result != 0) { 
        return result; 
       } 
      } 
      return 0; 
     } 
    } 

Collections.sort(list, 
       OpportunitySorter.getBaseComparator(SortDefinition)); 
0

为了方便使用,我用杰克逊和我运行JDK-8。这是代码的工作版本。 相关性:

<dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-core</artifactId> 
     <version>2.8.4</version> 
    </dependency> 
    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-databind</artifactId> 
     <version>2.8.4</version> 
    </dependency> 


import com.fasterxml.jackson.databind.ObjectMapper; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

public class JsonSorter { 
    public static void main(String[] args) throws Exception { 

     //1.Music 2.Games 3.Regional 4.English news 5.Entertainment 
     final Map<String, Integer> customSort = new HashMap<String, Integer>(); 
     customSort.put("Music", new Integer(1)); 
     customSort.put("Games", new Integer(2)); 
     customSort.put("Regional", new Integer(3)); 
     customSort.put("English news", new Integer(4)); 
     customSort.put("Entertainment", new Integer(5)); 

     String jsonString = "{\"category\": [{ \"Id\":\"23\" , \"Genre\":\"English News\" },{ \"Id\":\"43\" , \"Genre\":\"Entertainment\" },{ \"Id\":\"255\" , \"Genre\":\"Music\" },{ \"Id\":\"553\" , \"Genre\":\"Games\" },{ \"Id\":\"97\" , \"Genre\":\"regional\" }]}"; // Load this in whatever way makes sense for you 
     ObjectMapper mapper = new ObjectMapper(); 
     Map<String, Object> parsedJson = mapper.readValue(jsonString, Map.class); 
     List<Map<String, String>> genres = (List) parsedJson.get("category"); 
     genres.stream().sorted((thisMap, thatMap) -> customSort.get(thisMap.get("Genre")).compareTo(customSort.get(thatMap.get("Genre")))); 
     genres.stream().forEach(System.out::println); 
    } 
} 
相关问题