简体   繁体   中英

what is the best way to sort index and value of number of items

I have the following business requirement I have value1 and value2 pairs for an object and I need to save value1 and value2 in a sorted data structure according to value 2.

What I have done is making an array of hashmaps so that each element in the array contains a hasmap of two elements; value1 and value2. but I am stuck in the sorting mechanism, i need to sort the array according to the second value (value2)

Example

HashMap[] array = new HashMap[100]();

for(int i = 0; i < array.length; i++){
   array [i].put("value1", value1);
   array [i].put("value2", value2);
}

I am not sure if this is the best way to store this so I want the best data structure to use and sort accordingly but the main thing that I need to keep track of value1 so that it stick with value2 when sorting according to value2

I need to keep track of value1 so that it stick with value2 when sorting according to value2

@Thomas may be right that what you need is a sorted map. The limitation there is that SortedMap only sorts by the keys of the map which have to be unique. You can't put two things into the map with the same value2 contents.

An alternative is to create an object to encapsulate your two values. That's how we often do things in an object oriented language.

private static class ValueWrapper {
    private final String value1;
    private final String value2;
    public ValueWrapper(String value1, String value2) { 
        this.value1 = value1;
        this.value2 = value2;
    }
}

Then you can create a collection of these:

List<ValueWrapper> wrappers = new ArrayList<>();
wrappers.add(new ValueWrapper("1", "a"));
wrappers.add(new ValueWrapper("2", "b"));

If you want to sort them, you can either make the object itself implement Comparable or sort it using an external Comparator . So you could change the object:

private static class ValueWrapper implements Comparable<ValueWrapper> {
    private final String value1;
    private final String value2;
    public ValueWrapper(String value1, String value2) { 
        this.value1 = value1;
        this.value2 = value2;
    }
    public int compareTo(ValueWrapper other) {
        // we are comparing the value2 fields here
        return this.value2.compareTo(other.value2);
    }
}

Then you can do:

List<ValueWrapper> wrappers = new ArrayList<>();
wrappers.add(new ValueWrapper("1", "a"));
wrappers.add(new ValueWrapper("2", "b"));
// this sorts objects that implement Comparable
Collections.sort(wrappers);

As an alternative, you can provide an external Comparator class. This is useful if you sort the list in different ways:

// this uses an anonymous Comparator class but you can define a class to do it
Collections.sort(wrappers, new Comparator<ValueWrapper>() {
     public int compare(ValueWrapper o1, ValueWrapper o2) {
         return o1.value2.compareTo(o2.value2);
     }
});

You can then define a couple different Comparator classes, each comparing the ValueWrapper in different ways.

Sounds like you want a SortedMap implementation instead, probably a TreeMap :

SortedMap<String, String> values = new TreeMap<String, String>();
values.put(value2, value1); // value2 is the "key", value1 is the "value"

according to java doc "it does not guarantee that the order will remain constant over time " so if sort this HashMap may change it order ,i recommend to use class to store data in it,

public class test { public type1 a; public type2 b; }

so you can use default java sorting algorithm like this

Arrays.sort(array, new Comparator<test>() {
    @Override
    public int compare(test o1, test o2) {
        if(o1.a>o2.a)return 1;
        else if(o1.a==o2.a)return 0;
        return -1;
    }
});

Let's implement a helper class which implements Comparator :

public class MyComparator implements Comparator {
    public int compare(HashMap[] a, HashMap b) {
        String aVal = a.get("value2");
        String bVal = b.get("value2");
        if ((aVal == null) || (bVal == null)) {
            return 0;
        }
        return aVal.compareTo(bVal);
    }
}

and use it like this:

Arrays.sort(array, new MyComparator());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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