How do I sort an ArrayList of strings by alphabetical order?

21,357

Solution 1

kindly follow this way, don't make code too much stuff, here i see you have mess up you code and it makes difficult to understand easily.

kindly follow this easiest way to sort Dog Object's list in sorting order(Ascending/Descending) way.

import java.util.ArrayList;
import java.util.Collections;


class Dog implements Comparable<Dog>{
    String name;

    Dog(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int compareTo(Dog o) {
        return this.name.compareTo(o.getName()); // dog name sort in ascending order 
        //return o.getName().compareTo(this.name); use this line for dog name sort in descending order 
    }

    @Override
    public String toString() {
        return this.name;
    }
}


public class DogSort {

    public static void main(String[] args) {
        ArrayList<Dog> listDog = new ArrayList<Dog>();
        listDog.add(new Dog("doggy"));
        listDog.add(new Dog("aaa"));
        listDog.add(new Dog("bbb"));
        System.out.println(listDog);
        Collections.sort(listDog);
        System.out.println(listDog);

    }

}

Solution 2

Use

Collections.sort(dogList, new Comparator<Dog>(){
    public int compare(Dog d1, Dog d2){
         return d1.getName().compareTo(d2.getName());
    }    
});

This will compare each dogs based on comparator. Comparator simply compares the names of dogs.

Update: For idiomatic java 8 style

Collections.sort(dogList, (d1,d2) -> d1.getName().compareTo(d2.getName()));

Solution 3

You can sort it with:

  Collections.sort(list);

Solution 4

You'll need to implement the Comparator interface.

Solution 5

You use the same name DogList, for your class and the variable. Change it.

For example:

private ArrayList<Dog> innerList;

in your DogList class, declare a sort method

public sort() { 
Collections.sort(innerList);
}

and your Dog class must be comparable

public class Dog implements Comparable {

public int compareTo(Object _other)
{
// DO the comparaison
return 1;
}

}
Share:
21,357
Neo
Author by

Neo

Updated on July 09, 2022

Comments

  • Neo
    Neo almost 2 years

    I'm trying to create a program that can sort the contents of an ArrayList alphabetically. Right now I have three classes in my program...

    Dog

    public class Dog {
        private String name;
    
        public Dog(){
    
        }
    
        public void setName(String name){
            this.name = name;
        }
    
        public String getName(){
            return this.name;
        }
    }
    

    DogList (which contains the ArrayList of type Dog)

    import java.util.ArrayList;
    
    public class DogList {
    
        private ArrayList<Dog> dogList;
    
        public DogList(){
            DogList = new ArrayList<>();
        }
    
        public void setSize(int DogSize){
            for(int x = 0; x <= DogSize; x++){
                DogList.add(new Dog());
            }
        }
    
        public ArrayList<Dog> getList(){
            return dogList;
        }
    }
    

    and the last class, DogSorter, which is trying to access DogList ArrayList and then trying to sort the contents of that ArrayList in alphabetical order.

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    
    public class DogSorter {
        private DogList list = new DogList();
    
        public void sortDogs{
            Collections.sort(list);
        }
    
        for(i = 0; i < list.length(); i++) {
            System.out.println(list.getList().get(i).getName();
        }
    }
    

    I apologize for the long post. My question is simply: How do I sort the contents of the ArrayList accurately? I keep getting an error saying that I'm trying to compare the wrong types with my "Collections.sort" line. If anyone can help out, please let me know. Thank you.

    edit: To be clear, I'm trying to sort them alphabetically by the string stored in getName() for each object