bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Data Structures
Java•Java Data Structures

Java List Sorting

Native lesson simulator

Linear search

Java List Sorting

Linear searchtarget = 11701219211334i

Check index 0: 7 is not 11.

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java List Sorting?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ java.util.ArrayList;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Sort an ArrayList of Integers numerically in ascending order:
Sort an ArrayList of Strings alphabetically in ascending order:
Another useful class in the java.

Java Sort a List

Another useful class in the java.util package is the Collections class, which include the sort() method for sorting lists alphabetically or numerically.

Sort an ArrayList

Sort an ArrayList of Strings alphabetically in ascending order:

Example

import java.util.ArrayList;
import java.util.Collections;  // Import the Collections class
public class Main {
  public static void main(String[] args) {
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    Collections.sort(cars);  // Sort cars
    for (String i : cars) {
      System.out.println(i);
    }
}
}

Sort an ArrayList of Integers numerically in ascending order:

Example

import java.util.ArrayList;
import java.util.Collections;  // Import the Collections class
public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> myNumbers = new ArrayList<Integer>();
    myNumbers.add(33);
    myNumbers.add(15);
    myNumbers.add(20);
    myNumbers.add(34);
    myNumbers.add(8);
    myNumbers.add(12);
    Collections.sort(myNumbers);  // Sort myNumbers
    for (int i : myNumbers) {
      System.out.println(i);
    }
}
}

Reverse the Order

You can also sort a list in reverse order, by using the reverseOrder() method.

In the following example, we sort an ArrayList of Strings alphabetically in reverse/descending order:

Example

import java.util.ArrayList;
import java.util.Collections;  // Import the Collections class
public class Main {
  public static void main(String[] args) {
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    Collections.sort(cars, Collections.reverseOrder()); // Sort cars
    for (String i : cars) {
      System.out.println(i);
    }
}
}

Sort an ArrayList of Integers numerically in reverse/descending order:

Example

import java.util.ArrayList;
import java.util.Collections;  // Import the Collections class
public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> myNumbers = new ArrayList<Integer>();
    myNumbers.add(33);
    myNumbers.add(15);
    myNumbers.add(20);
    myNumbers.add(34);
    myNumbers.add(8);
    myNumbers.add(12);
    Collections.sort(myNumbers, Collections.reverseOrder()); // Sort myNumbers
    for (int i : myNumbers) {
      System.out.println(i);
    }
}
}

Previous

Java LinkedList

Next

Java Set