简体   繁体   English

如何根据另一个数组的顺序对对象数组进行排序

[英]How to sort array of objects based on order of another array

I have an ArrayList of Strings in a certain particular order我有一个 ArrayList 以特定顺序排列的字符串

ArrayList<String> sortedkeys

And I have an unordered set of objects我有一组无序的对象

Set<Skugroup> unsortedSet

Where SkuGroup contains a key to be used for sorting其中 SkuGroup 包含用于排序的键

Class SkuGroup {
   private String sortkey;
   private String name; 
}

I need to copy all the SkuGroup objects into a new array in the same order of sortedSkus, where the sortedkey value is the same as the SkuGroup->sortkey我需要将所有 SkuGroup 对象按照 sortedSkus 的相同顺序复制到一个新数组中,其中 sortedkey 值与 SkuGroup->sortkey 相同

How can I accomplish this?我怎样才能做到这一点?

I believe the best way would be to store the set as a TreeSet , because it keeps its elements sorted by a given comparator.我相信最好的方法是将集合存储为TreeSet ,因为它保持其元素按给定比较器排序。 To make it easier, here we can implement interface Comparable on the SkuGroup class.为了方便起见,这里我们可以在SkuGroup class 上实现接口Comparable

class SkuGroup implements Comparable {
    private String sortKey;
    private String name;

    public String getSortKey() {
        return sortKey;
    }

    public int compareTo(SkuGroup s1) {
        return this.sortKey.trim().compareTo(s1.getSortKey().trim());
    }
}
ArrayList<String> sortedKeys = new ArrayList<>();

Set<SkuGroup> skuSet = new TreeSet<>();

/** add items to skuSet **/

I need to copy all the SkuGroup objects into a new array in the same order of sortedSkus, where the sortedkey value is the same as the SkuGroup->sortkey我需要将所有 SkuGroup 对象按照 sortedSkus 的相同顺序复制到一个新数组中,其中 sortedkey 值与 SkuGroup->sortkey 相同

I assume you mean you want to transfer just the sortKey , and not the entire object because of the type on the ArrayList.我假设您的意思是您只想传输sortKey ,而不是整个 object 因为 ArrayList 上的类型。

skuSet.stream().forEach(item -> sortedKeys.add(item.getSortKey()));
import java.util.*;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) {
        System.out.println("Expected order: c -> b -> a -> d");
        List<String> sortedkeys = Arrays.asList("c", "b", "a", "d");

        // Construct unsorted set (of course a Set has no order);
        System.out.println("Unsorted order: a -> b -> c -> d");
        Set<SkuGroup> unsortedSet = new HashSet<>(Arrays.asList(
                new SkuGroup("a", "name_a"),
                new SkuGroup("b", "name_b"),
                new SkuGroup("c", "name_c"),
                new SkuGroup("d", "name_d")));

        List<SkuGroup> sortedSkuGroups = sortedkeys.stream()
                .map(key -> unsortedSet.stream()
                        .filter(skuGroup -> skuGroup.getSortKey().equals(key))
                        .findFirst()
                        .get()
                ).collect(Collectors.toList());
        System.out.println("Sorted order: " + sortedSkuGroups);

        // In case if you need to convert this into Array:
        SkuGroup[] sortedSkuGroupArray = sortedSkuGroups.stream().toArray(SkuGroup[]::new);
        System.out.println("Sorted order in Array: " + Arrays.toString(sortedSkuGroupArray));
    }
}

class SkuGroup {
    private String sortKey;
    private String name;

    public SkuGroup(String sortKey, String name) {
        this.sortKey = sortKey;
        this.name = name;
    }

    public String getSortKey() {
        return sortKey;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "{'" + sortKey + '\'' + ":'" + name + "\'}";
    }
}

Result:结果:

> Task :Test.main()
Expected order: c -> b -> a -> d
Unsorted order: a -> b -> c -> d
Sorted order: [{'c':'name_c'}, {'b':'name_b'}, {'a':'name_a'}, {'d':'name_d'}]
Sorted order in Array: [{'c':'name_c'}, {'b':'name_b'}, {'a':'name_a'}, {'d':'name_d'}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM