Naomi
Naomi

Reputation: 139

Sorting arraylist with numbers in ascending form

al.add("1 Romeo and Juliet");
al.add("2 Juliet");
al.add("3 Romeo");
al.add("4 Mercutio");
al.add("5 Tybalt");
al.add("6 Nurse");
al.add("7 Robert Smith");
al.add("8 The Cure");
al.add("9 Suede");
al.add("10 Neil Codling");

I want to add al.add("11 Brett Anderson") on that arraylist, but everytime I do that, the output is always like this:

1 Romeo and Juliet
10 Neil Codling
11 Brett Anderson
2 Juliet
3 Romeo
4 Mercutio
5 Tybalt")
6 Nurse
7 Robert Smith
8 The Cure
9 Suede

what I want to happen is the numbers are should be in order, from 1 - 11. what code should I put aside from Collections.sort(arrayname);?

Upvotes: 0

Views: 149

Answers (7)

Alexis C.
Alexis C.

Reputation: 93842

You can create a class to handle this:

class Book {
   private int id;
   private String name;
   //other stuff
}

Then just add a new Book to your list:

al.add(new Book(1,"Romeo and Juliet"));

And just sort it using a custom comparator:

Collections.sort(al, new Comparator<Book>(){
        @Override
        public int compare(Book b1, Book b2) {
            return Integer.compare(b1.getId(), b2.getId());  
        }
};


Another way would be to use a TreeMap<Integer, String> where each id is mapped to one book name.

If you really want to continue with your current approach (which I don't recommend) you could split your String by whitespaces and take the first element in the array returned by split (which assumed that your String is always in the format (number whateverIsAfter)).

Collections.sort(al, new Comparator<String>(){
    @Override
    public int compare(String b1, String b2) {
         String s1 = b1.split("\\s+")[0];
         String s2 = b2.split("\\s+")[0];
         return Integer.valueOf(s1).compareTo(Integer.valueOf(s2)); 
    }
});

Upvotes: 2

robermann
robermann

Reputation: 1722

Use a java.util.TreeMap, the number as the key, the string as the value. It will be sorted by the number "natural" order.

Upvotes: 0

ruhungry
ruhungry

Reputation: 4696

You can use Map and keep as a key number and title as a value

http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

Upvotes: 0

rsudha
rsudha

Reputation: 307

you can use a Comparator passed as an argument to the Collections.sort(arrayname, compartor instance) to sort the ArrayList based on your criteria.

Upvotes: 0

Brian
Brian

Reputation: 6450

Your problem is you're sorting alphabetically right now, not numerically, so "11..." does come before "2...". You need to parse up until the first space character, extract that content into a number, then sort by that. This should of course check that "content before first space" is a number.

All of this might sound tricky, but by writing a new 'TitleNumberComparator' it shouldn't be too bad.

Learn about comparators, and comparable: http://javarevisited.blogspot.co.uk/2011/06/comparator-and-comparable-in-java.html

Upvotes: 0

Savv
Savv

Reputation: 431

Can you use a TreeSet instead of ArrayList? The TreeSet's structure sorts anything you put on it automatically. Also, create a class that takes the number and name separately and add those in your list instead. Don't forget to implement Comparable or a Comparator

Upvotes: 0

Salah
Salah

Reputation: 8657

Use Collections.sort with a proper Comparator

something like:

Collections.sort(ar, new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            // TODO Auto-generated method stub
            return o1.compareTo(o2);
        }
    });

Upvotes: 0

Related Questions