sorting objects in arraylist in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shahidasalavoodeen
    New Member
    • Oct 2006
    • 1

    #1

    sorting objects in arraylist in java

    How can I sort objects in an arraylist based on a particular field of the object.
    my arraylist has a collection of objects. each object has one of the fields as name. now i want to sort the objects in alphabetical order of names. how do i do dat
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shahidasalavood een
    How can I sort objects in an arraylist based on a particular field of the object.
    my arraylist has a collection of objects. each object has one of the fields as name. now i want to sort the objects in alphabetical order of names. how do i do dat
    If you can control how objects get into the list, you can make sure that the objects get in sorted(insertio n sort). If you already have an unsorted list, then you need to write the sort function to sort that. Are you asking for the sort function?

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      This will allow you as many sorts as are compare methods defined for your object:
      Code:
      import java.util.*;
      class Church {
      	private String name;
      	private String pastor;
      	public Church(String name, String pastor) {
      		this.name = name;
      		this.pastor = pastor;
      	}
      	public String getPastor() {
      		return pastor;
      	}
      	public String getName() {
      		return name;
      	}
      	public void setPastor(String pastor) {
      		this.pastor = pastor;
      	}
      	public String toString() {
      		return getName() + " is Pastored by "+getPastor();
      	}
      	public int compareByPastor(Church c) {
      		int x = pastor.compareTo(c.getPastor());
      		return x;
      	}
      	public int compareByName(Church c) {
      		int x = name.compareTo(c.getName());
      		return x;
      	}
      }
      
      class Churches {
      	private final List<Church> churches;
      
      	public Churches() {
      		churches = new ArrayList<Church>();
      	}
      	public void addWithoutSorting(Church c) {
      		churches.add(c);
      	}
      
      	//You could always add using this method
      	public void addWithSorting(Church c) {
      
      	}
      	public void display() {
      		for(int j = 0; j < churches.size(); j++) {
      			System.out.print(churches.get(j).toString());
      			System.out.println("");
      		}
         }
         public List<Church> getChurches() {
      	   return churches;
         }
         public void sortBy(String s) {
      	   for (int i = 1; i < churches.size(); i++) {
      		   int j;
      		   Church val = churches.get(i);
                 for (j = i-1; j > -1; j--) {
      			   Church temp = churches.get(j);
      			   if(s.equals("Pastor")) {
      				   if (temp.compareByPastor(val) <= 0) {
      					   break;
      				   }
      			   }
      			   else if(s.equals("Name")) {
      				   if (temp.compareByName(val) <= 0) {
      				   	   break;
      				   }
      			   }
      			   churches.set(j+1, temp);
      			}
      			churches.set(j+1, val);
             }
           }
      
          public static void main(String[] args) {
      		Churches baptists = new Churches();
      	    baptists.addWithoutSorting(new Church("Pac", "Pastor G"));
      	    baptists.addWithoutSorting(new Church("New Life", "Tudor"));
      	    baptists.addWithoutSorting(new Church("My Church", "r035198x"));
      	    baptists.addWithoutSorting(new Church("AFM", "Cathy"));
      	    System.out.println("**********************Before Sorting***********************");
      	    baptists.display();
      	    baptists.sortBy("Pastor");
      	    System.out.println("**********************After sorting by Pastor**************");
      	    baptists.display();
      	    baptists.sortBy("Name");
      	    System.out.println("**********************After sorting by Name****************");
      	    baptists.display();
      
      	}
      
        }

      Comment

      Working...