@jorge Вы можете использовать Collections.sort()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.ArrayList; import java.util.Collections; class Main { public static void main(String[] args) throws Exception { ArrayList booksCosts = new ArrayList(); booksCosts.add(15); booksCosts.add(13); booksCosts.add(21); booksCosts.add(7); System.out.println("Before : "); for (int i = 0; i < booksCosts.size(); i++) { System.out.print(booksCosts.get(i) + " "); } Collections.sort(booksCosts); System.out.println("\nAfter : "); for (int i = 0; i < booksCosts.size(); i++){ System.out.print(booksCosts.get(i) + " "); } // Вывод : // Before : // 15 13 21 7 // After : // 7 13 15 21 } } |