Results 1 to 5 of 5
  1. #1
    yankeefan9874 is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default Need help with secondary sort

    Hey guys,

    I'm working on a simple program that sorts and prints the ArrayList in ABC order by States then secondary sort by City if there are repeated states. I got the list to order by State but how would I be able to do the secondary sort for City?

    Thanks for the help.

    Java Code:
    import java.util.*;
    import java.io.*;
    import java.text.*;
    
    public class SortIt15
    {
       public static void main(String [] args)
       {
    		// Load the entire array list
          ArrayList<Customer> custList = CustomerList.getCustomers();
    
    		for( Customer info : custList )
    			System.out.println( info );
    		
          Comparator<Customer> comp = new ZipComparator();
          Collections.sort( custList, comp );
        
          System.out.println("\n- - Sorted by ID:");
    		for( Customer info : custList )
    			System.out.println( info );
    
          Comparator<Customer> comp2 = new StateCityComparator();
          Collections.sort( custList, comp2 );
    					
          System.out.println("\n- - Sorted by State City:");
    		for( Customer info : custList )
    			System.out.println( info );
    
       } // end main
    } // end SortIt15
    
    class ZipComparator implements Comparator<Customer>
    {
       public int compare( Customer first, Customer second )
       {
          int fir = first.getZip();
          int sec = second.getZip();
          
          //  return fir-sec;
          
          if ( fir < sec  ) return -1;
          if ( fir == sec ) return 0;
          return 1;
          
       } // end compare()
    } // end ZipComparator
    
    class StateCityComparator implements Comparator<Customer>
    {
    	public int compare(Customer first, Customer second)
    	{
    		String firstState = first.getState();
    		String secondState = second.getState();
    		
    		String firstCity = first.getCity();
    		String secondCity = second.getCity();
    		
    		char stateChar = firstState.charAt(0);
    		char stateChar2 = secondState.charAt(0);
    		char cityChar = firstCity.charAt(0);
    		char cityChar2 = secondCity.charAt(0);
    	        
                    //Sort by State
    		if(stateChar < stateChar2) return -1;
    		if(stateChar == stateChar2) return 0;
    		return 1;
    	}
    }
    
    class Customer
    {
    	private String address;
            private int    zip;
    	private String city;
    	private String state;   
       public Customer( String _address, String _city, String _state, int _zip )
       {
    		address 	= _address;
    		city		= _city;
    		state		= _state;
    		zip		= _zip;
       } // end Customer constructor
    
    	public String getAddress()	{ return address;	}
       public String getCity()		{ return city;		}
    	public String getState()	{ return state;	}
    	public int getZip()      	{ return zip;     }
        
       public String toString()
       {
          return String.format("%-30s %-20s %4s %09d",
    			getAddress(), getCity(), getState(), getZip() );
       } // end toString()
       
       
    } // end class Customer
    
    class CustomerList {
    
    	public static ArrayList<Customer> getCustomers() {
    		ArrayList<Customer> custList = new ArrayList<Customer>();
    		
    		custList.add(new Customer("132 Commerical Way","Cleveland","OH",442340000) );
    		custList.add(new Customer("2421 West Industrial Way","Berkeley","CA",947100000) );
    		custList.add(new Customer("2421 West Industrial Way","Berkeley","CA",947100000) );
    		custList.add(new Customer("4223 Halster Way","Berkeley","CA",947101234) );
    		custList.add(new Customer("4223 Halster Way","Berkeley","CA",947104321) );
    		custList.add(new Customer("1 Washington Complex","Boston","MA",21010000) );
    		custList.add(new Customer("1 Braddock Circle","Kenmore","NY",142230000) );
    		custList.add(new Customer("1 Braddock Circle","Kenmore","NY",142230000) );
    		custList.add(new Customer("1 Braddock Circle","Kenmore","NY",142230000) );
    		custList.add(new Customer("45511 Delaware Ave.","Buffalo","NY",142210000) );
    		custList.add(new Customer("3112 West Helm Street","Kingwood","TX",773390000) );
    		custList.add(new Customer("3112 West Helm Street","Kingwood","TX",773390000) );
    		custList.add(new Customer("41 Golem Terrace","New York","NY",100120000) );
    		custList.add(new Customer("1602 Jackson Ave.","Arlington","VA",222020000) );
    		custList.add(new Customer("8871 West Grange Drive","Cairo","NY",124130000) );
    		custList.add(new Customer("44 WhiteStallion Pike","Lindenwold","NJ",80210000) );
    		custList.add(new Customer("45A Sturgeon Dr., Bldg. 5","Ft. Pierce","FL",349510000) );
    		custList.add(new Customer("45A Sturgeon Dr., Bldg. 5","Ft. Pierce","FL",349510000) );
    		custList.add(new Customer("776 West Ninth St.","Myrtle Beach","SC",295790000) );
    		custList.add(new Customer("6665 Peachtree Lane","Atlanta","GA",303280000) );
    		custList.add(new Customer("3 Mapleview Drive","Huntsville","AL",358030000) );
    		custList.add(new Customer("67 Merrifield Ave.","Oceanside","NY",115720000) );
    		custList.add(new Customer("35 West 9th St.","New York","NY",100120000) );
    		custList.add(new Customer("35 West 9th St.","New York","NY",100120000) );
    		custList.add(new Customer("5531 E. Lansing Ave.","Ypsilanti","MI",481970000) );
    		custList.add(new Customer("7554 West 9th St.","New York","NY",100030000) );
    		custList.add(new Customer("4 Rocky Way","Colorado Springs","CO",809410000) );
    		custList.add(new Customer("4 Rocky Way","Colorado Springs","CO",809410000) );
    		custList.add(new Customer("45521 Pilgrim Circle","Nantucket","MA",25540000) );
    		custList.add(new Customer("3 Carnegie Circle","Pittsburgh","PA",151080000) );
    		custList.add(new Customer("15 Carnegie Circle","Pittsburgh","PA",151080000) );
    		custList.add(new Customer("16 Broad Street","Rochester","NY",146210000) );
    		custList.add(new Customer("16 Broad Street","Rochester","NY",146210000) );
    		custList.add(new Customer("5665 MassPike Circle","Sandy Hook","CT",64820000) );
    		custList.add(new Customer("3 Desert Trail","Las Vegas","NV",891170000) );
    		custList.add(new Customer("3 Confederate Ave.","Roanoke","VA",240100000) );
    		custList.add(new Customer("44 Rockman Blvd.","Rochester","NY",146230000) );
    
    		return custList;
    	}
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    10

    Default

    When comparing two strings why not just use the string classes built in compareTo method and rely on the oracle developers to have done it correctly?

  3. #3
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    6

    Default

    Java Code:
    class StateCityComparator implements Comparator<Customer>
    {
    	public int compare(Customer first, Customer second)
    	{
    		String firstState = first.getState();
    		String secondState = second.getState();
    		
    		String firstCity = first.getCity();
    		String secondCity = second.getCity();
    		
    		int stateCompare = firstState.compareToIgnoreCase( secondState );
    		if( stateCompare < 0 )return -1;
    		if( stateCompare > 0 )return 1;
    		if( stateCompare == 0 ){
    			int cityCompare = firstCity.compareToIgnoreCase( secondCity );
    			if( cityCompare < 0)return -1;
    			if( cityCompare == 0)return 0;
    			if( cityCompare > 0)return 1;
    		}
    		throw new RuntimeException( "ProgrammingError" );
    	}
    }

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    10

    Default

    this can very easily be changed and simplified to something like
    Java Code:
    int stateCompare = firstState.compateToIgnoreCase(secondState);
    if(stateCompare == 0){
      return firstCity.compareToIgnoreCase(secondCity);
    }
    return stateCompare;
    This should work correctly as well.

  5. #5
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    6

    Default

    I'm impressed.
    I was distracted while OP's comparator returned -1,0,1, but that is unnessessary indeed.

Similar Threads

  1. Secondary JFrame
    By jitheshmenon in forum AWT / Swing
    Replies: 7
    Last Post: 08-09-2010, 11:56 PM
  2. Java not recognizing UNIX secondary group
    By PaulBinni in forum Networking
    Replies: 1
    Last Post: 11-18-2009, 04:45 PM
  3. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 02:03 AM
  4. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  5. changing the secondary ID of a view
    By schuetzejanett in forum SWT / JFace
    Replies: 0
    Last Post: 08-08-2007, 07:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •