Java linear table sorting sample sharing _java

Source: Internet
Author: User

You can take a look at this static method public static <T> void sort (list<t> List, comparator< super t> C)

1. Define a model first:

Copy Code code as follows:

Package model;

/**
* User.java
*
* @author Beam WP March 3, 2014
*/
public class User
{
Private String UserName;
private int userage;

Public User ()
{
}

Public User (String userName, int userage)
{
This.username = UserName;
This.userage = Userage;
}

Public String GetUserName ()
{
return userName;
}

public void Setusername (String userName)
{
This.username = UserName;
}

public int Getuserage ()
{
return userage;
}

public void setuserage (int userage)
{
This.userage = Userage;
}
}

2. Then define a comparator, implement the Java.util.Comparator interface, and write a comparison rule in the Compare () method:

Copy Code code as follows:

Package util;

Import Java.util.Comparator;

Import model. User;

/**
* Comparatoruser.java
*
* @author Beam WP March 3, 2014
*/
public class Comparatoruser implements Comparator<user>
{
@Override
public int Compare (user arg0, user arg1)
{
Compare names first.
int flag = Arg0.getusername (). CompareTo (Arg1.getusername ());

If the name is the same, compare the age
if (flag = 0)
{
Return Arg0.getuserage ()-arg1.getuserage ();
}
return flag;
}
}

3. Sort by using java.util.Collections inside the sort (list list, Comparator C) method:

Copy Code code as follows:

Package test;

Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.List;

Import Util.comparatoruser;
Import model. User;

/**
* Testapp.java
*
* @author of the March 3, 2014
*/
public class TESTAPP
{
public static void Main (string[] arg0)
{
list<user> userlist = new arraylist<user> ();

Inserting data
Userlist.add (New User ("A", 15));
Userlist.add (New User ("B", 14));
Userlist.add (New User ("A", 14));

Sort
Collections.sort (userlist, New Comparatoruser ());

Print results
for (User u:userlist)
{
System.out.println (U.getusername () + "" + u.getuserage ());
}
}
}

4. Operation Result:

Copy Code code as follows:

A 14
A 15
B 14

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.