1

I have code:

    List<ModelMap> nameCustomer = new ArrayList<ModelMap>();

    for (int i=1;i<150;i++) {
                    ModelMap map = new ModelMap();
                    map.put("name", "nazwa " + i);
                    map.put("surname", "nazwisko " + i);
                    map.put("number", "123 ");
                    nameCustomer.add(map);
                }

            ModelMap[] result = new ModelMap[nameCustomer.size()];
            ModelMap[] rowsArray = nameCustomer.toArray(result);

How can I sort nameCustomer (or rowsArray) by surname or number?

2
  • Look at Arrays.sort() for the array and Collections.sort() for the list. Commented Dec 3, 2012 at 13:05
  • possible duplicate of Sort ArrayList of custom Objects by property Commented Dec 3, 2012 at 13:05

5 Answers 5

1

You can use the Collections.sort(nameCustomer, comparator) method to sort your list. For that you have to write the code for your Comparator.The code for Comparator will be -

public Comparator<ModelMap> getComparator(final String sortBy){
   if ("surname".equals(sortBy)) {
   return new Comparator<ModelMap>() {
   @Override int compare(ModelMap m1, ModelMap m2) 
    return m1.getSurname().compareTo(m2.getSurname());
   }};} 
     else if (condition) {// ...  }
      else {
        throw new IllegalArgumentException("invalid sort field '" + sortBy + "'"); }
       }

I am assuming that you have a getSurname method written in your ModelMap class to get the surname. If not then write it to make the above code work.

You can visit here for further help. Cheers !!

Sign up to request clarification or add additional context in comments.

2 Comments

I don't have method in ModelMap class. How should be write this method?
public string getSurname(){return surname} write this in your ModelMap class
1

You need to implement a Comparator that defines your sorting criteria,
then you can use Arrays.sort for arrays, or Collections.sort for Lists, or dump everything into a SortedSet (e.g., TreeSet)

2 Comments

How should be write my Comparator?
@faszynski all of those questions you have can be answered by a very simple google search...
0

You'll probably want to use the Collections.Sort package. Lists maintain "natural ordering" to maintain consistency. e.g.

    List<Name> names = Arrays.asList(nameArray);
    Collections.sort(names);
    System.out.println(names);

4 Comments

But how to do that if you have a two-level List/Map or similar structure ? The only solution I see is to implement a custom iterator to return a specified sublevel-key, then extract it as an array, then finally sort it.
As the other answers state, you should use a custom Comparator.
So this list does not answer the question because it tells OP that Collections.sort() does use natural ordering, which is not what he wants...
Yes, I read the doc one more time, and effectively it is easily possible to implement such a thing, and smarter to do it this way.
0

using the Colections.sort() and give it a custom Comparator class implementation, which will do a string compare by surname.

Or use a LinkedHashMap, which has consistency after put() ang get() and keys sorted.

I hope it helps!

2 Comments

How should be write my Comparator?
Exactly what I wrote in profile: the fish needed ( the complete source coe ready to copy-paste) not the fishing knowledge...Not even an upvote for accepted answer, what a shame!
0

You can implement your own iterator on List, which could be parametrized to return only the inner key you specified.

Then, "iterating" with a standard sorter around it does the trick.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.