简体   繁体   中英

Sort .txt data in a special way in java

I have a.txt file that i wanna sort that looks like this:

GENEBOI INC COM 15893X590   46,741  174,700 SH      DFND    4,11    174,700 0   0
DCHATRA RESTAURATION GROUP INC N    CL A    14771P827   2,659,074   5,213,461   SH      DFND    4,8,11 5,213,461    0   0
FROLAKATZ CO    COM 107393852   35,744  800,000 SH      DFND    4   800,000 0   0

I want to sort every line where the second number in each line is the only important thing for the sort algorithm. I wanna sort them from the highest (eg 2,659,074) to the lowest(eg 35,744). The important part is that every line's content should not get edited or mixed up with other lines.

So the result should look like this:

DCHATRA RESTAURATION GROUP INC N    CL A    14771P827   2,659,074   5,213,461   SH      DFND    4,8,11 5,213,461    0   0
GENEBOI INC COM 15893X590   46,741  174,700 SH      DFND    4,11    174,700 0   0
FROLAKATZ CO    COM 107393852   35,744  800,000 SH      DFND    4   800,000 0   0

Assuming tab-delimited records, it would be something like this:

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        System.out.println("Missing input and output filenames.");
        return;
    }
    List<String> lines = Files.readAllLines(Path.of(args[0]), StandardCharsets.UTF_8);
    NumberFormat nf = NumberFormat.getInstance();
    Collections.sort(lines, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            String[] f1 = o1.split("\t");
            String[] f2 = o2.split("\t");
            if (f1.length >= 3 && f2.length >= 3) {
                try {
                    Number n1 = nf.parse(f1[2]);
                    Number n2 = nf.parse(f2[2]);
                    return Long.compare(n2.longValue(), n1.longValue());
                } catch (ParseException ex) {
                    // ignored
                }
            }
            return 0;
        }
    });
    Files.write(Path.of(args[1]), lines, StandardCharsets.UTF_8,
            StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
}

run with <input_file> <output_file> as arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM