Java implementation Voctor to sort the samples in a specified way share _java

Source: Internet
Author: User

Copy Code code as follows:

Import java.util.*;
Class Mycompare implements Comparator//implementation Comparator, define your own comparison method
{
public int Compare (object O1, Object O2) {
Elem e1= (Elem) O1;
Elem e2= (Elem) O2;

if (E1.get () > E2.get ()) The comparison is descending, if 1 to 1 is ascending.
{
return-1;
}
else if (E1.get () <e2.get ())
{
return 1;
}
Else
{
return 0;
}
}
}
Class Elem {
private int ival;
Public Elem (int i) {
This.ival = i;
}
public int get () {
return this.ival;
}
}
public class Vector1 {
public static void Main (string[] args) {
List v = new Vector ();
V.add (New Elem (1));
V.add (New Elem (22));
V.add (New Elem (3));
V.add (New Elem (14));
Comparator ct = new Mycompare ();
Collections.sort (v, CT);
for (int i = 0; i < v.size (); i++)
System.out.println (((Elem) v.get (i)). get ());
}
}

Related Article

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.