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

How to sort JsonArray based on multiple values in java?

我有一个 JsonArray,它有大量的 "id" 和 "Genre",这将是 random.The 示例 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.Entertainment) 这个顺序。

如何对这个 json 数组进行排序?有没有第三方库可以使用?

任何帮助将不胜感激:)

您必须在此处编写多个比较器,即为您要排序的每个字段编写一个比较器,然后按您想要的顺序链接这些比较器。将这些比较器列表传递给基本比较器,在比较方法中,您将在这些比较器上循环并调用它们的比较方法。如下所示:

[伪代码]

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

您可以创建多个比较器,并且可以在排序时使用任何比较器。

  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)

为了方便使用,我用过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);
    }
}