How do i sort By Month + Day in java

Discussion in 'OT Technology' started by D1G1T4L, Dec 25, 2003.

  1. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    So i wrote a function which sorts people by first name

    Code:
    import java.util.*;
    
    public class PeopleDataGroup extends ArrayList implements Comparator
    {
    	public PeopleDataGroup()
    	{
    		super();
    		
    	}
    	public void sortByFirstName()
    	{
    		Collections.sort(this, this);
    
    	}
    	public final int compare(Object a, Object b)
    	{
    		String firstName = ((PeopleData)a).getFirstName();
    		String firstName2 = ((PeopleData)b).getFirstName();
    		return (((String)firstName).toLowerCase().compareTo(((String) firstName2).toLowerCase()));
    		
    	} // end compare 
    
    }
     
     
    but now i need a way to sort by Month of birth and Day

    since Months are not in alphabetical order, i need some good way to do this
     
  2. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Unless Java has some fancy builtin date functions, easiest way is probably to build a lookup table to convert each month into its number then sort that way.
     
  3. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    yea i was thinking of converting each month into a number..... like April would be 4
     
  4. aphoric

    aphoric Even if god did exist, it would be necessary to ab

    Joined:
    Aug 29, 2003
    Messages:
    918
    Likes Received:
    0
    Location:
    Leaving Afghanistan
    Create a java.util.Date for their birthday and compare it.
     
  5. ph1

    ph1 Guest

    you look a little confused on the use of comparators for sorting collections. the 'proper' method is to implement a comparator and use it to sort your collection. the collection cannot be a comparator. they are two seperate entities.

    for example;

    Code:
    ArrayList dateList = new ArrayList();
    dateList.add(new Date(System.currentTimeMillis()));
    dateList.add(new Date(System.currentTimeMillis() - 10000));
    dateList.add(new Date(System.currentTimeMillis() + 10000));
    		
    Comparator dateComp = new Comparator() {
    	public int compare(Object o1, Object o2) {
    		if (o1 instanceof Date && o2 instanceof Date) {
    			return ((Date)o1).compareTo((Date)o2);
    		}
    		throw new ClassCastException();
    	}
    			
    	public boolean equals(Object obj) {
    		if (obj instanceof Comparator) {
    			return obj.equals(this);
    		}
    		return false;
    	}
    };
    		
    System.out.println("before sort");
    System.out.println(dateList.toString());
    		
    Collections.sort(dateList, dateComp);
    		
    System.out.println("after sort");
    System.out.println(dateList.toString());
    
    In your case you should make a PeopleBlah object, populate the arraylist with these objects. Then modify the comparator compareTo() method to compare the date members of your PeopleBlah object.
     

Share This Page