Вы находитесь на странице: 1из 3

12/16/2016

HowdoIsortacollectionorarrayofstrings?WebTutorialsavajava.com

Search Tutorials:

Go

AVAJAVA Web Tutorials


Total Categories: 24, Total Tutorials: 508
General Java: 54 of 69 tutorials
How do I sort a collection or array of strings?

Web Tutorials ::

General Java

Tutorial Categories:

::

54. How do I sort a collection or array of strings?

How do I sort a collection or array of strings?

1. Ajax (1)

Author: Deron Eriksson

2. Ant (16)

Description: This tutorial shows how to sort an array or collection of Strings.

3. Apache Web Server (8)

Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse

4. Bioinformatics (10)

3.3.0)

5. Cascading Style Sheets (47)


6. Classes and Objects (14)
7. Database (13)
8. Design Patterns (22)
9. Eclipse (39)

Java

makes sorting very easy. As an example of this, if you have an array of

strings, you can sort the strings via a call to Arrays.sort(), such as:
Arrays.sort(stringArray);

10. Files (62)


11. General Java (69)
12. JSPs (9)
13. Java Basics (11)
14. Linux (23)
15. Logging (5)
16. Maven (88)
17. Search (12)

If you require the Strings to be sorted without regards to case, you'll need a
second argument, a Comparator, for Arrays.sort(). Such a comparator has already
been written for us and can be accessed as a static on the String class named
CASE_INSENSITIVE_ORDER.
Arrays.sort(stringArray,String.CASE_INSENSITIVE_ORDER);

18. Servlets (20)


19. Struts (1)

Similarly if you have a collection of strings, you can sort the collection via calls

20. Text (19)

to:

21. Tomcat (8)


22. Version Control (8)

Collection.sort(stringCollection);

23. Windows (2)


24. XML (1)

and
Collection.sort(stringCollection,String.CASE_INSENSITIVE_ORDER);

The StringSortExample class gives examples of sorting an array of strings and a


list of strings. The array and collection are sorted with regards to case and then
without regards to case.

http://www.avajava.com/tutorials/lessons/howdoisortacollectionorarrayofstrings.html

1/3

12/16/2016

HowdoIsortacollectionorarrayofstrings?WebTutorialsavajava.com

StringSortExample.java
packageexample;

importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.Collections;
importjava.util.List;

publicclassStringSortExample{

publicstaticvoidmain(String[]args)throwsException{

String[]strArray={"Carol","bob","Alice"};

displayArray(strArray);

Arrays.sort(strArray);

displayArray(strArray);

Arrays.sort(strArray,String.CASE_INSENSITIVE_ORDER);

displayArray(strArray);

System.out.println("");

List<String>strList=newArrayList<String>();

strList.add("larry");

strList.add("Moe");

strList.add("Curly");

displayList(strList);

Collections.sort(strList);

displayList(strList);

Collections.sort(strList,String.CASE_INSENSITIVE_ORDER);

displayList(strList);

publicstaticvoiddisplayArray(String[]array){

for(Stringstr:array){

System.out.print(str+"");

System.out.println();

publicstaticvoiddisplayList(List<String>list){

for(Stringstr:list){

System.out.print(str+"");

System.out.println();

The console output is shown here. Notice after the first sort of the array and the
first sort of the collection, the results have been alphabetized but case matters
(uppercase words come before lowercase words). Notice that after the second
sort of the array and the second sort of the collections, the results have been
alphabetized without regards to case.

http://www.avajava.com/tutorials/lessons/howdoisortacollectionorarrayofstrings.html

2/3

12/16/2016

HowdoIsortacollectionorarrayofstrings?WebTutorialsavajava.com

Console Output
CarolbobAlice
AliceCarolbob
AlicebobCarol

larryMoeCurly
CurlyMoelarry
CurlylarryMoe

If you require other types of sorting, you can write your own Comparator, where
you define your sorting logic in the compare() method of the Comparator
interface.

Web Tutorials ::

General Java

::

54. How do I sort a collection or array of strings?

Copyright 2014 Code Strategies | Template: Free CSS Templates | Contact

http://www.avajava.com/tutorials/lessons/howdoisortacollectionorarrayofstrings.html

3/3

Вам также может понравиться