0

I have an arraylist defined whose elements are say, [man, animal, bird, reptile]. The elements in the arraylist are non-mandatory. The list can even be empty.

I always need to give the output as [animal,man,reptile,bird]. Means, the order of the elements are to be maintained. Is there any way of doing in arraylist?

I thought I can do like

for (String listElement: customList) { //custom list variable holds all elements
    if (listElement.equalsIgnoreCase("animal"){
        newList.add(0, listElement);
} else if("man") {
    newlist.add(1, listElement);
}

But I would want to know the best practice of doing. Can someone please help me on this?

5
  • 3
    what are you trying to do? I don't see any obvious logic in your ordering example. Commented Dec 9, 2013 at 10:47
  • You can use TreeSet with a custom comparator. ;) Commented Dec 9, 2013 at 10:49
  • I want to order the elements in custom way. From [man, animal, bird, reptile] to [animal, man, reptile, bird]. The order is pre-defined. Commented Dec 9, 2013 at 10:50
  • @Poppy Does it follow a certain logic ? Commented Dec 9, 2013 at 10:51
  • @ZouZou No. Ther is no logic behind. It is just pre-defined order. Commented Dec 9, 2013 at 10:57

4 Answers 4

2

You can define a custom sorting and use it to order your array (save the comparator somewhere, so you don't have to instantiate it many times):

List<String> definedOrder = // define your custom order
    Arrays.asList("animal", "man", "reptile", "bird");

Comparator<String> comparator = new Comparator<String>(){

    @Override
    public int compare(final String o1, final String o2){
        // let your comparator look up your car's color in the custom order
        return Integer.valueOf(definedOrder.indexOf(o1))
            .compareTo(Integer.valueOf(definedOrder.indexOf(o2)));
    }
};

Collections.sort(myList, comparator);
Sign up to request clarification or add additional context in comments.

Comments

1

Use a custom comparator:

    Collections.sort(customList, comparator);

    int i = 0;
    for (String temp : customList) {
        System.out.println("customList " + ++i + " : " + temp);
    }

Custom comparator below:

public static Comparator<String> comparator = new Comparator<String>() {        

    public int compare(String str1, String str2) {
        return orderOf(str1) - orderOf(str2);
    }

    private int orderOf(String name) {          
            return ((List)Arrays.asList("animal", "man", "reptile", "bird")).indexOf(name);
    }
};

Comments

0

You can use Collections.sort(yourArrayList, new CustomComparator());

You need to create your own comparator for this, though, but it is easy.

public class CustomComparator implements Comparator<YourType>{

    @Override
    public int compare(YoyrType o1, YoyrType o2) {
        // logic for ordering the list
    }
} 

Comments

0

arraylist is ordered.
maybe you want to insert element into the list.
could you create a new list every time when you have to insert and copy each of them?

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.