Java Object array Sort _java

Source: Internet
Author: User
Tags array sort comparable

Not much nonsense said directly to the code first:

Import java.util.*;
Import java.io.*;

public class main{
static int [] DP = new int [1010];
public static void Main (String [] args) throws ioexception{
Mouse [] mice = new Mouse [1010];
FileReader fr=new FileReader ("In.txt");  Read the file
bufferedreader read = new BufferedReader (FR);
String str = "";
int n=1;
while ((str = read.readline ())!=null) {
String [] s= str.split ("");
Mice[n] = new Mouse ();  Object instantiation, very important
mice[n].weight = Integer.parseint (S[0]);
Mice[n].speed =integer.parseint (s[1]);
n++;
}
SYSTEM.OUT.PRINTLN (n);
Arrays.sort (mice,1,n);  Sort (int start,int end) includes the start index, excluding the end index for
(int i=1;i<n;i++) {
System.out.println (mice[i].weight+ "" +mice[i].speed);
}} Class Mouse implements comparable{  //Implements comparable interface
int weight;
int speed;
public int compareTo (Object o) {   //rewrite CompareTo method
Mouse m= (Mouse) o;
Return weight>m.weight?1: (weight==m.weight?0:-1);
}

Another attached arrays.sort usage:

1. Number sort int[] Intarray = new int[] {4, 1, 3,-23};

Arrays.sort (Intarray);
Output: [-23, 1, 3, 4]

  2. String sorting , first uppercase after lowercase string[] strarray = new string[] {"Z", "a", "C"};

Arrays.sort (Strarray);
Output: [C, A, z]

  3. Strictly alphabetical order, that is, ignore the case of the sort case-insensitive sort

Arrays.sort (Strarray, String.case_insensitive_order);
Output: [A, C, z]

  4. Reverse sorting , Reverse-order sort

Arrays.sort (Strarray, Collections.reverseorder ());
Output: [Z, A, C]

  5. Ignore case Reverse sort case-insensitive reverse-order sort

Arrays.sort (Strarray, String.case_insensitive_order);
Collections.reverse (Arrays.aslist (Strarray));
Output: [Z, C, a]

  6, Object array sorting

To sort an array of objects, you implement the Java.util.Comparator interface yourself
Example:
Common_user[] Userlisttemp=new common_user[temp.size ()];
Arrays.sort (Userlisttemp, New Pinyincomparator ());
Pinyincomparator implements the comparator interface and rewrites the Compare method to tell arrays what rules to compare the size of two objects.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.