Usage of the comparator interface in Java

Source: Internet
Author: User
/*** Function: Sort an integer array to understand comparator usage ** @ author wangchangshuai0010 Shandong University of Science and Technology February 7, 2013 */package test; import Java. io. bufferedinputstream; import Java. util. arrays; import Java. util. comparator; import Java. util. extends; public class test {private class mycomparator implements comparator <integer >{@ overridepublic int compare (integer O1, integer O2) {If (O1> O2) {return 1 ;} else if (O1 <O2) {return-1 ;}else {return 0 ;}} public static void main (string [] ARGs) {test main = new test (); mycomparator mcomparator = Main. new mycomparator (); partition CIN = new partition (New bufferedinputstream (system. in); system. out. println ("Enter the length of the array:"); int COUNT = cin. nextint (); integer a [] = new integer [count]; system. out. println ("Please input this" + Count + "number:"); For (INT I = 0; I <. length; I ++) {A [I] = cin. nextint ();} arrays. sort (A, mcomparator); system. out. println ("the sorting result is as follows:"); For (INT I = 0; I <. length; I ++) {system. out. print (A [I] + "");}}}

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.