pedja
pedja

Reputation: 3413

Sorting list by value

I have a List<String> and a List<Integer>. Both are in specific order(they are linked). List<String> contains names and List<Integer> their values.

Is there a way to sort List<Integer> by size but also change ordering of List<String> so that values and names stays linked?

Upvotes: 5

Views: 6024

Answers (5)

Maddy Sharma
Maddy Sharma

Reputation: 4956

You can try this :

Create a custom Comprator class like :

import java.util.Comparator;

/**
 * Created by Maddy Sharma on 7/17/2015.
 * Contains sorting logic to sort Dialogs by its date(long)
 */
public class DateDialogComparator implements Comparator<UserChatDialog>{

// lhs.getLastMessageDateSent() is the long value

    @Override
    public int compare(UserChatDialog lhs, UserChatDialog rhs) {
    // This line will work in java 7 and Android API Level 19
    //    return Long.compare(lhs.getLastMessageDateSent(), rhs.getLastMessageDateSent());


                              'OR'


        if(lhs.getLastMessageDateSent()>rhs.getLastMessageDateSent())
            return -1;
        else if(lhs.getLastMessageDateSent()<rhs.getLastMessageDateSent())
            return +1;
        return 0;
    }
}

call it from activity or fragment like :

ArrayList<UserChatDialog> myList = userChatDialogList;
Collections.sort(myList, new DateDialogComparator());
Log.i("List after sorting is:", "" + userChatDialogList);

Upvotes: 0

ppeterka
ppeterka

Reputation: 20726

From a clean code perspective, if these belong together, and this structure can be reused somewhere else, you should create a class out of them, and make it implement Comparable :

public SomeClass implements Comparable<SomeClass> {

    private Integer id;
    private String name;

    private Whatever elseIsNeededHere;

    /* getters'n'setters*/

    // ******** THOU SHALT NEVER FORGET THY FRIENDS, EQUALS AND HASHCODE, OR ELSE THEY TURN TO BE THY FOES ********
    @Override
    public boolean equals(Object other) { //do what it takes }

    @Override
    public int hashCode() { //do what it takes! }

    /* actually implement Comparable */
    @Override 
    public int compareTo(SomeClass o) {
        //null check, and other bloat left out for sake of brevity
        return this.id.compareTo(o.getId());
    }

}

Then you can have a single list containing what belongs together, and sort it really nicely:

ArrayList<SomeClass> myList = getMyData();
Collections.sort(myList);

And the myList list instance is sorted the way you wanted.

If you want different comparation methods, you can use the other way, by using Comparators:

Collections.sort(myList,new Comparator<SomeClass>() { 
    public int compare(SomeClass a, SomeClass b) { // do what it takes }
    });

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691645

You should use a List<NameAndValue> instead of two lists (and find a better name than NameAndValue, which would reflect what these data actually represent). Java is an OO language. Use objects. That's what they're for: containing data that are related, and providing behaviour with methods and encapsulation.

Once you have this class, you'll be able to sort your list by name, value or both, and adding an additional field if needed won't be a problem.

Upvotes: 14

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

- You can use java.util.TreeMap<Integer, String> which which implements the SortedMap. It sort in natural ordering of the key.

- You can also use java.util.Comparator<T> to sort it out.

Upvotes: 2

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

You need java.util.TreeMap<Integer,String> . This sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

Upvotes: 15

Related Questions