Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement dictionary sorting in java

Shulou Source: shulou.com Published: 2022-06-02 10:42:47 10月02日 Update

It is believed that many inexperienced people don't know what to do about how to achieve dictionary sorting in java. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Import java.util.Comparator;import java.util.ArrayList;import java.util.Collections;public class Tester {public static void main (String [] args) {ArrayList list = new ArrayList (); list.add ("East Bay"); list.add ("Aolai"); list.add ("East Bay-Cave"); list.add ("Aolai Pharmacy") / * * use Collections's sort () method to sort it. The sort () method needs to pass a series of parameters, one is the Collection to be sorted and the other is a Comparator * / Collections.sort (list, new SpellComparator ()); for (int I = 0; I < list.size ()) {System.out.println (list.get (I)) } / * Chinese character Pinyin sorting comparator * / class SpellComparator implements Comparator {public int compare (Object o1, Object O2) {try {/ / obtains the Chinese character code of the comparison object and converts it into the string String S1 = new String (o1.toString (). GetBytes ("GB2312"), "ISO-8859-1"); String S2 = new String (o2.toString (). GetBytes ("GB2312"), "ISO-8859-1") / use the compareTo () method of the String class to compare the two objects: return s1.compareTo (S2);} catch (Exception e) {e.printStackTrace ();} return 0;}} after reading the above, have you mastered how to sort the dictionary in java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Sorting methods dictionaries contents objects more questions Donghai Chinese characters helpless for this reasons parameters characters strings for this caves skills pinyin articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Huawei Apple MariaDB MySQL