如何根据java中的多个值对JsonArray进行排序?

时间:2016-11-07 04:39:54

标签: java arrays json jsonobject

我有一个JsonArray,它有大量的“id”和“Genre”,这将是随机的。示例json数组如下:

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

我需要根据以下内容对jsonarray进行排序:

(1.Music 2.Games 3.Regional 4.English新闻 5.娱乐)这个命令。

如何让这个json数组排序?是否有任何第三方库使用?

任何帮助将不胜感激:)

3 个答案:

答案 0 :(得分:0)

你必须在这里编写多个比较器,比如你要排序的每个场的比较器,然后按照你想要的顺序链接那些比较器。将这些比较器列表传递给基本隔离区,在比较方法中,您将在这些比较器上循环并调用它们的比较方法。如下所示:

[Pseduo Code]

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));

答案 1 :(得分: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());
                  }
            };
}

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

 Collections.sort(myList, ComparatorsGroup.GenreComparator);

有关详细信息,请参阅MkYong的以下示例。

Java object sorting example (Comparable and Comparator)

答案 2 :(得分:0)

为了便于使用,我使用了Jackson,我正在运行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);
    }
}