'Printing out the integer numbers in ascending order from list of lists that have different lengths

public static void ascendingOrder(List<List<Integer>> listOfListOfNums) {
    String result = "";
    int index = 0;

    while (!listOfListOfNums.isEmpty()) {
        int smallestNumber = Integer.MAX_VALUE;
        for (int i = 0; i < listOfListOfNums.size(); ++i) {
            if (listOfListOfNums.get(i).size() == 0) {
                listOfListOfNums.remove(i);
            } else if (smallestNumber > listOfListOfNums.get(i).get(0)) {
                smallestNumber = listOfListOfNums.get(i).get(0);
                index = i;
            }}
    if(!listOfListOfNums.isEmpty()){
        result += smallestNumber + " ";
       listOfListOfNums.get(index).remove(0);
    }
        }
    System.out.println(result);

This is the code I have so far, however, it stops finding the smallest number at some point and just prints out Integer.maxValue().



Solution 1:[1]

Using java 8 stream based approach,  
   private static List<Integer> ascendingOrder(List<List<Integer>> listList) {
            return listList.stream().flatMap(List::stream).sorted().collect(Collectors.toList());
        }

Solution 2:[2]

You need to iterate over ALL the integers, every single time, to find the next biggest number. You were only ever checking the first number in each set.

Bear in mind that the approach you're taking is completely destroying the original list!

Fixed code:

  public static void main(String[] args) {       
    List<List<Integer>> data = new ArrayList<List<Integer>>();
    List<Integer> list = new ArrayList<Integer>();
    list.add(4);
    list.add(5);
    list.add(6);
    data.add(list);
    list = new ArrayList<Integer>();
    list.add(7);
    list.add(8);
    list.add(10);
    data.add(list);
    list = new ArrayList<Integer>();
    list.add(1);
    list.add(3);
    list.add(6);
    data.add(list);
    list = new ArrayList<Integer>();
    list.add(11);
    list.add(22);
    list.add(33);
    data.add(list);
    list = new ArrayList<Integer>();
    data.add(list);

    System.out.println("Input:");
    for(List<Integer> l : data) {
      System.out.println(l);
    }
    List<Integer> result = ascendingOrder(data);
    System.out.println("Output:");
    System.out.println(result);
  }

  public static List<Integer> ascendingOrder(List<List<Integer>> listOfListOfNums) {
    int set = 0;
    int index = 0;
    List<Integer> result = new ArrayList<Integer>();
    do {
      set = -1;
      index = -1;
      int smallestNumber = -1;
      for (int i = 0; i < listOfListOfNums.size(); i++) {
        List<Integer> list = listOfListOfNums.get(i);
        for(int j = 0; j < list.size(); j++) {
          int number = list.get(j);
          if (set == -1 || number < smallestNumber) {
            smallestNumber = number;
            set = i;
            index = j;
          }  
        }        
      }
      if(set != -1){
        result.add(smallestNumber);
        listOfListOfNums.get(set).remove(index);
      }
    } while (set != -1);
    listOfListOfNums.clear();
    return result;
  }

Example output:

Input:
[4, 5, 6]
[7, 8, 10]
[1, 3, 6]
[11, 22, 33]
[]
Output:
[1, 3, 4, 5, 6, 6, 7, 8, 10, 11, 22, 33]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohan k
Solution 2 Idle_Mind