Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse Java list sorting

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to sort the analysis of Java list for you, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The Collections.sort method is mainly used for Java list sorting here:

Package com.tom.compare; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class CompareClient {/ * * @ param args * / public static void main (String [] args) {/ / TODO Auto-generated method stub List list = new ArrayList (); list.add (new Content (15000, "1asdfasd5000")); list.add (new Content (10000, "10000")) List.add (new Content (20000, "20000")); list.add (new Content (30000, "30000")); list.add (new Content (25000, "25000")); list.add (new Content (13000, "20000")); list.add (new Content (15000, "20000")); list.add (new Content (89000, "89000")); ContentComparator comp = new ContentComparator (); Collections.sort (list,comp) Content content; for (int I = 0; I

< list.size(); i++){ content = (Content)list.get(i); System.out.println(" content.getName() " + content.getName()); } } } package com.tom.compare; import java.util.Comparator; public class ContentComparator implements Comparator { public int compare(Object o1, Object o2) { // TODO Auto-generated method stub Content c1 = (Content) o1; Content c2 = (Content) o2; if (c1.getKey() >

C2.getKey () {return 1;} else {if (c1.getKey () = = c2.getKey ()) {return 0;} else {return-1;} package com.tom.compare; public class Content {private long key; private String name; public Content (long key, String name) {this.key = key; this.name = name } public long getKey () {return key;} public void setKey (long key) {this.key = key;} public String getName () {return name;} public void setName (String name) {this.name = name;}}

The result is:

Content.getName () 10000 content.getName () 13000 content.getName () 1asdfasd5000 content.getName () 15000 content.getName () 20000 content.getName () 25000 content.getName () 30000 content.getName () 89000

The following is sorted by time:

Public static void sss () {String [] dates = {"2 Dec 2003 12:12:05", "2 Apr 2003 13:12:05", "2 Jan 2003 10:12:05" "2 Feb 2003 15:12:05",} Java.text.SimpleDateFormat f = new java.text.SimpleDateFormat ("d MMM y HH:mm:ss", Locale.ENGLISH); try {System.out.println ("before:"); for (int I = 0; I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report