Amazing technological breakthrough possible @S-Logix pro@slogix.in

Office Address

  • 2nd Floor, #7a, High School Road, Secretariat Colony Ambattur, Chennai-600053 (Landmark: SRM School) Tamil Nadu, India
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to sort numbers list of items in java?

Description

This program shows how to sort the integer and string using sort method of Arrays class that sorts the elements in natural order (Integer-ascending order, String-Alphabetical order). As the sort method is static the input array is modified instead of creating new array. Sorted elements can be accessed using enhanced for loop.

Sample Code
  • Filename: Sorting.java

import java.util.*;
class Sorting {
public static void main(String args[]) {
//Sorting Integer in natural order
int n[] = {40,20,29,65,74};
Arrays.sort(n);
System.out.println("Sorted list of Integer");
for(int i:n) {
System.out.println(i);
}
//Sorting String in natural order
String s[] = {"dog","cat","animal","bird","dove"};
Arrays.sort(s);
System.out.println("Sorted list of String");
for(String i:s) {
System.out.println(i);
}
}
}

Screenshots

Sort numbers list of items in java