The Artima Developer Community
Sponsored Link

Java Buzz Forum
how to sort arraylist of strings alphabetically java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
how to sort arraylist of strings alphabetically java Posted: Mar 10, 2016 2:50 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: how to sort arraylist of strings alphabetically java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
1.Basic Java example program to sort arraylist of strings

  1. package com.javasortarraylistofobjects;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Iterator;
  6.  
  7. public class SortArrayList{
  8.  
  9. public static void main(String[] args) {
  10.   
  11. //create an ArrayList object
  12. ArrayList<String> arrayList = new ArrayList();
  13.        
  14. //Add elements to Arraylist
  15.         
  16. arrayList.add("A"); 
  17. arrayList.add("C");
  18. arrayList.add("D");
  19. arrayList.add("Z");
  20. arrayList.add("F");
  21. arrayList.add("J");
  22. arrayList.add("K");
  23. arrayList.add("M");
  24. arrayList.add("L");
  25. arrayList.add("O");
  26.        
  27.         
  28.  System.out.println("Before sorting ArrayList ...");
  29.  Iterator itr=arrayList.iterator();
  30.         
  31. while (itr.hasNext()) {
  32.  
  33. System.out.println(itr.next());
  34.      
  35. }
  36.  
  37.        
  38.  /*
  39.  To sort an ArrayList object, use Collection.sort method. This is a
  40.   static method. It sorts an ArrayList object's elements into ascending order.
  41. */
  42.   Collections.sort(arrayList);
  43.      
  44.   System.out.println("After sorting ArrayList ...");
  45.        
  46.     
  47.         
  48. Iterator itr1=arrayList.iterator();
  49.         
  50. while (itr1.hasNext()) {

  51. System.out.println(itr1.next());
  52.             
  53. }
  54.     
  55.   
  56. }
  57. }
     




Output:

  1. Before sorting ArrayList ...
  2. A
  3. C
  4. D
  5. Z
  6. F
  7. J
  8. K
  9. M
  10. L
  11. O
  12. After sorting ArrayList ...
  13. A
  14. C
  15. D
  16. F
  17. J
  18. K
  19. L
  20. M
  21. O
  22. Z

Read: how to sort arraylist of strings alphabetically java

Topic: Search an element of Java ArrayList Example Previous Topic   Next Topic Topic: Java Tutorial : Java String(primitive Numbers to Strings-valueOf method)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use