The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java : Collection Framework : Collections (Sort List of Country Objects in desc Order Based on Name)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java : Collection Framework : Collections (Sort List of Country Objects in desc Order Based on Name) Posted: Jul 2, 2015 7:14 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java : Collection Framework : Collections (Sort List of Country Objects in desc Order Based on Name)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube : 
https://www.youtube.com/watch?v=AhMA81hEEH4&list=UUhwKlOVR041tngjerWxVccw

Country.java
public class Country
{
private int countryId;
private String countryName;

public Country(int countryId, String countryName)
{
super();
this.countryId = countryId;
this.countryName = countryName;
}

public int getCountryId()
{
return countryId;
}

public void setCountryId(int countryId)
{
this.countryId = countryId;
}

public String getCountryName()
{
return countryName;
}

public void setCountryName(String countryName)
{
this.countryName = countryName;
}

}
CountrySortByNameComparator.java
import java.util.Comparator;

public class CountrySortByNameComparator implements Comparator<Country>
{

/*
* This method has logic to arrange the country objects in descending order
* based on Country Name.
*/


@Override
public int compare(Country country1, Country country2)
{

System.out
.println("Compare method in CountrySortByNameComparator has been called"
+ "\nin order to arrange the country objects in descending order \nbased on Country Name.\n");

return country2.getCountryName().compareTo(country1.getCountryName());

}

}
CollectionsSortExample.java
import java.util.ArrayList;
import java.util.Collections;

/*
* Example of sort(List<T> list, Comparator<? super T> c) method
*/


public class CollectionsSortExample
{

public static void main(String[] args)
{
Country india = new Country(1, "Bangladesh");
Country china = new Country(4, "China");
Country usa = new Country(3, "Australia");
Country srilanka = new Country(2, "Denmark");

ArrayList<Country> countryList = new ArrayList<Country>();
countryList.add(india);
countryList.add(china);
countryList.add(usa);
countryList.add(srilanka);

System.out.println("Before Sort : \n");

for (Country country : countryList)
{
System.out.println("Country Id: " + country.getCountryId() + " || "
+ "Country Name: " + country.getCountryName());
}

System.out.println("-------------------------------------------------");
/*
* Sorts the specified list according to the order induced by the
* specified comparator.
*/

Collections.sort(countryList, new CountrySortByNameComparator());

System.out.println("-------------------------------------------------");
System.out.println("\nAfter Sort : \n");

for (Country country : countryList)
{
System.out.println("Country Id: " + country.getCountryId() + " || "
+ "Country Name: " + country.getCountryName());
}

}

}
Output
Before Sort  : 

Country Id: 1 || Country Name: Bangladesh
Country Id: 4 || Country Name: China
Country Id: 3 || Country Name: Australia
Country Id: 2 || Country Name: Denmark
-------------------------------------------------
Compare method in CountrySortByNameComparator has been called
in order to arrange the country objects in descending order
based on Country Name.

Compare method in CountrySortByNameComparator has been called
in order to arrange the country objects in descending order
based on Country Name.

Compare method in CountrySortByNameComparator has been called
in order to arrange the country objects in descending order
based on Country Name.

Compare method in CountrySortByNameComparator has been called
in order to arrange the country objects in descending order
based on Country Name.

Compare method in CountrySortByNameComparator has been called
in order to arrange the country objects in descending order
based on Country Name.

-------------------------------------------------

After Sort :

Country Id: 2 || Country Name: Denmark
Country Id: 4 || Country Name: China
Country Id: 1 || Country Name: Bangladesh
Country Id: 3 || Country Name: Australia
To Download CollectionsSortDemoSortCountryByNameDesc Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/CollectionsSortDemoSortCountryByNameDesc.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Read: Java : Collection Framework : Collections (Sort List of Country Objects in desc Order Based on Name)

    Topic: Java : Collection Framework : Collections - Playlist Previous Topic   Next Topic Topic: Java : Collection : Stack - Playlist

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use