3

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

Neo
  • 397
  • 1
  • 4
  • 15
  • Btw: why do you post code which is obviously not the code you're working with? Please avoid that. – Tom Dec 06 '15 at 10:07

7 Answers7

8

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()));
Nayanjyoti Deka
  • 419
  • 3
  • 15
7

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

    }

}
Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55
4

You can sort it with:

  Collections.sort(list);
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
2

You'll need to implement the Comparator interface.

ifly6
  • 5,003
  • 2
  • 24
  • 47
2

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

}
2

You can use Comparator to solve this ..

I hope this will solve your issue - How to sort an ArrayList in Java

Community
  • 1
  • 1
Ali
  • 427
  • 1
  • 4
  • 14
  • And how should your advice work? Neither is `list` an array nor does it contain one. – Tom Dec 06 '15 at 10:10
1

Either implement the Comparable interface in your Dog class or write a custom Comparator which would sort Dog on their name.

Amit
  • 383
  • 3
  • 15