By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,364 Members | 1,637 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,364 IT Pros & Developers. It's quick & easy.

sorting objects in arraylist in java

P: 1
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
Oct 26 '06 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
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(insertion 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?
Oct 27 '06 #2

10K+
P: 13,264
This will allow you as many sorts as are compare methods defined for your object:
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. class Church {
  3.     private String name;
  4.     private String pastor;
  5.     public Church(String name, String pastor) {
  6.         this.name = name;
  7.         this.pastor = pastor;
  8.     }
  9.     public String getPastor() {
  10.         return pastor;
  11.     }
  12.     public String getName() {
  13.         return name;
  14.     }
  15.     public void setPastor(String pastor) {
  16.         this.pastor = pastor;
  17.     }
  18.     public String toString() {
  19.         return getName() + " is Pastored by "+getPastor();
  20.     }
  21.     public int compareByPastor(Church c) {
  22.         int x = pastor.compareTo(c.getPastor());
  23.         return x;
  24.     }
  25.     public int compareByName(Church c) {
  26.         int x = name.compareTo(c.getName());
  27.         return x;
  28.     }
  29. }
  30.  
  31. class Churches {
  32.     private final List<Church> churches;
  33.  
  34.     public Churches() {
  35.         churches = new ArrayList<Church>();
  36.     }
  37.     public void addWithoutSorting(Church c) {
  38.         churches.add(c);
  39.     }
  40.  
  41.     //You could always add using this method
  42.     public void addWithSorting(Church c) {
  43.  
  44.     }
  45.     public void display() {
  46.         for(int j = 0; j < churches.size(); j++) {
  47.             System.out.print(churches.get(j).toString());
  48.             System.out.println("");
  49.         }
  50.    }
  51.    public List<Church> getChurches() {
  52.        return churches;
  53.    }
  54.    public void sortBy(String s) {
  55.        for (int i = 1; i < churches.size(); i++) {
  56.            int j;
  57.            Church val = churches.get(i);
  58.            for (j = i-1; j > -1; j--) {
  59.                Church temp = churches.get(j);
  60.                if(s.equals("Pastor")) {
  61.                    if (temp.compareByPastor(val) <= 0) {
  62.                        break;
  63.                    }
  64.                }
  65.                else if(s.equals("Name")) {
  66.                    if (temp.compareByName(val) <= 0) {
  67.                           break;
  68.                    }
  69.                }
  70.                churches.set(j+1, temp);
  71.             }
  72.             churches.set(j+1, val);
  73.        }
  74.      }
  75.  
  76.     public static void main(String[] args) {
  77.         Churches baptists = new Churches();
  78.         baptists.addWithoutSorting(new Church("Pac", "Pastor G"));
  79.         baptists.addWithoutSorting(new Church("New Life", "Tudor"));
  80.         baptists.addWithoutSorting(new Church("My Church", "r035198x"));
  81.         baptists.addWithoutSorting(new Church("AFM", "Cathy"));
  82.         System.out.println("**********************Before Sorting***********************");
  83.         baptists.display();
  84.         baptists.sortBy("Pastor");
  85.         System.out.println("**********************After sorting by Pastor**************");
  86.         baptists.display();
  87.         baptists.sortBy("Name");
  88.         System.out.println("**********************After sorting by Name****************");
  89.         baptists.display();
  90.  
  91.     }
  92.  
  93.   }
Oct 27 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.