0
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);?

2
  • Why not using a map or an array? Commented Mar 14, 2014 at 14:02
  • Use a treemap, the number as the key, the string as the value... Commented Mar 14, 2014 at 14:04

7 Answers 7

2

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)); 
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

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);
        }
    });

Comments

0

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

Comments

0

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

Comments

0

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

Comments

0

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

Comments

0

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

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.