Sort List of Strings arrays in Java

Tags: , , , ,



I’m trying to make a program that consists of an array of Strings . now I need to sort them in order ‘PJ’, ‘GHS’, ‘FJ’, ‘PAS’ and date.

input :

    listOfStringArrays.add(new String[] { "200608", "FJ" });
    listOfStringArrays.add(new String[] { "200608", "PJ" });

    listOfStringArrays.add(new String[] { "200609", "FJ" });
    listOfStringArrays.add(new String[] { "200609", "PJ" });

    listOfStringArrays.add(new String[] { "200610", "FJ" });
    listOfStringArrays.add(new String[] { "200610", "PJ" });
    listOfStringArrays.add(new String[] { "200610", "PAS" });
    listOfStringArrays.add(new String[] { "200610", "GHS" });

    listOfStringArrays.add(new String[] { "200609", "SHO" });
    listOfStringArrays.add(new String[] { "200615", "FIA" });

    listOfStringArrays.add(new String[] { "200611", "GHS" });
    listOfStringArrays.add(new String[] { "200615", "FIA" });
    listOfStringArrays.add(new String[] { "200609", "PAS" });
    listOfStringArrays.add(new String[] { "200607", "FIA" });

output:
//Always ‘PJ’, ‘GHS’, ‘FJ’ then ‘PAS’

    listOfStringArrays.add(new String[] { "200607", "FIA" });

    listOfStringArrays.add(new String[] { "200608", "PJ" });
    listOfStringArrays.add(new String[] { "200608", "FJ" });

    listOfStringArrays.add(new String[] { "200609", "PJ" });
    listOfStringArrays.add(new String[] { "200609", "FJ" });
    listOfStringArrays.add(new String[] { "200609", "PAS" });
    listOfStringArrays.add(new String[] { "200609", "SHO" });

    listOfStringArrays.add(new String[] { "200610", "PJ" });
    listOfStringArrays.add(new String[] { "200610", "GHS" });
    listOfStringArrays.add(new String[] { "200610", "FJ" });
    listOfStringArrays.add(new String[] { "200610", "PAS" });

    listOfStringArrays.add(new String[] { "200615", "FIA" });

Answer

You should consider using custom java classes instead of String Array. Find below an example to do it .

    public static void main(String[] args) {
        List<String[]> listOfStringArrays = getArrayOfStrings();
        new SortArrayOfStrings().sort(listOfStringArrays);

        listOfStringArrays.stream().forEach(s -> System.out.println(String.format("%s %s", s[0], s[1])));
    }

    private static List<String[]> getArrayOfStrings() {
        List<String[]> listOfStringArrays = new LinkedList<>();
        listOfStringArrays.add(new String[]{"200608", "FJ"});
        listOfStringArrays.add(new String[]{"200608", "PJ"});

        listOfStringArrays.add(new String[]{"200609", "FJ"});
        listOfStringArrays.add(new String[]{"200609", "PJ"});

        listOfStringArrays.add(new String[]{"200610", "FJ"});
        listOfStringArrays.add(new String[]{"200610", "PJ"});
        listOfStringArrays.add(new String[]{"200610", "PAS"});
        listOfStringArrays.add(new String[]{"200610", "GHS"});

        // listOfStringArrays.add(new String[]{"200609", "SHO"});
        //   listOfStringArrays.add(new String[]{"200615", "FIA"});

        listOfStringArrays.add(new String[]{"200611", "GHS"});
        //  listOfStringArrays.add(new String[]{"200615", "FIA"});
        listOfStringArrays.add(new String[]{"200609", "PAS"});
        //  listOfStringArrays.add(new String[]{"200607", "FIA"});
        return listOfStringArrays;
    }

    private void sort(List<String[]> listOfStringArrays) {
        Collections.sort(listOfStringArrays, new Comparator<>() {
            @Override
            public int compare(String[] o1, String[] o2) {
                if (o1.length != 2 && o2.length != 2) {
                    throw new IllegalArgumentException("String array contains more than two values");
                }
                int comp = getValue(o1[1]).compareTo(getValue(o2[1]));
                if (comp == 0) {
                    DateFormat df = new SimpleDateFormat("yyMMdd");
                    try {
                        return df.parse(o1[0]).compareTo(df.parse(o2[0]));
                    } catch (ParseException e) {
                        throw new IllegalArgumentException("Unable to parse date", e);
                    }
                }
                return comp;

            }
        });
    }

    public Integer getValue(String s) {
        // TODO add FIA and SHO
        List<String> values = Arrays.asList("PJ", "GHS", "FJ", "PAS");
        for (int i = 0; i < values.size(); i++) {
            if (values.get(i).equals(s)) {
                return i;
            }
        }
        return -1;
    }
}


Source: stackoverflow