ERLEDIGT
NEIN
NEIN
ANTWORTEN
3
3
ZUGRIFFE
532
532
EMPFEHLEN
-
Guten Tag!
Ich suche einen möglichst unkomplizierten Weg eine ASCII formatierte Datei zu sortieren.
Die Datei ist wie folgt aufgebaut:
1. Zeile: <double> <double> <int> ...
2. Zeile: <double> <double> <int> ...
.
.
Es soll nach der ersten Spalte sortiert werden.
Meine Idee war folgendes:
Code java:1 2 3 4 5 6 7 8 9 10
raf = new RandomAccessFile(filename, "r"); list = new ArrayList<String>(); do { line = raf.readLine(); list.add(line); } while (raf.getFilePointer() < raf.length()); raf.close(); Collections.addAll(list); Collections.sort(list);
So funktioniert´s leider nicht ganz, da nach Strings sortiert wird.
9.2 kommt z.b. nach 849.73...
Ich befürchte fast, dass ich um StringTokenizer, zwischenspeichern der Zeile und einzelnes sortieren der Werte aus der ersten Spalte nicht drumrum komm.
Hat jemand eine schnelle Lsg vorzuschlagen?
-
Hi,
falls möglich könntest du die Zeile, die du bei readLine() einliest mit .split(" "); bei allen Leerzeichen Trennen. Dann kannst du mithilfe von Integer.parseInt(String s); bzw. Double.parseDouble(String s); arbeiten, um die einzelnen Strings in Zahlen zu konvertieren. Danach müsste ein leicht modifizierter Algorythmus dein Problem lösen.
Gruß
taouri
-
Hallo Bob,
hier mal ein möglicher unvollständiger Ansatz:
Code java: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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
import java.io.*; import java.util.Arrays; import java.util.Comparator; public class SortFileExample { public static void main(String[] args) throws IOException { FileOutputStream fout = new FileOutputStream("a.txt"); DataOutputStream dout = new DataOutputStream(fout); FileInputStream fis = new FileInputStream("a.txt"); DataInputStream dis = new DataInputStream(fis); int lines = 19; for (int i = 0; i < lines; i++) { dout.writeInt(i % 3); dout.writeUTF("Test" + i); dout.writeLong(lines - i); } dout.close(); fout.close(); Object[][] o = new Object[lines][3]; for (int i = 0; i < lines; i++) { o[i][0] = dis.readInt(); o[i][1] = dis.readUTF(); o[i][2] = dis.readLong(); } dis.close(); fis.close(); System.out.println("Vor Sortierung:"); for (int i = 0; i < o.length; i++) { for (int j = 0; j < 3; j++) { System.out.print(o[i][j] + "\t"); } System.out.print("\n"); } Arrays.sort(o, new ArrayComparator(0)); System.out.println(); System.out.println("Nach Sortierung:"); for (int i = 0; i < o.length; i++) { for (int j = 0; j < 3; j++) { System.out.print(o[i][j] + "\t"); } System.out.println(); } Arrays.sort(o, new ArrayComparator(2)); System.out.println(); System.out.println("Nach 2. Sortierung:"); for (int i = 0; i < o.length; i++) { for (int j = 0; j < 3; j++) { System.out.print(o[i][j] + "\t"); } System.out.println(); } } static public class ArrayComparator implements Comparator { private int sortColumn = 0; public ArrayComparator(int sortColumn) { this.sortColumn = sortColumn; } public void setSortColumn(int sortColumn) { this.sortColumn = sortColumn; } public int compare(Object o1, Object o2) { if (o1 == null && o2 == null) return 0; if (o1 == null) return -1; if (o2 == null) return 1; Object[] d1 = (Object[]) o1; Object[] d2 = (Object[]) o2; return d1[sortColumn].toString().compareTo(d2[sortColumn].toString()); } } }
Vg Erdal
-
Hy, Erdal
du kannst mir vielleicht helfen.
ich habe ein sortierproblem, unswar muss ich Tabellen sortieren.
Part: 1 (47)
-3.605202648226e-001 -9.327513809436e-001 0.000000000000e+000
9.327513809436e-001 -3.605202648226e-001 0.000000000000e+000
0.000000000000e+000 0.000000000000e+000 1.000000000000e+000
8.330508804029e-021 5.711266338201e-021 0.000000000000e+000
Part: 2 (59 39)
1.000000000000e+000 0.000000000000e+000 0.000000000000e+000
0.000000000000e+000 1.000000000000e+000 0.000000000000e+000
0.000000000000e+000 0.000000000000e+000 1.000000000000e+000
0.000000000000e+000 0.000000000000e+000 0.000000000000e+000
Part: 1 (47)
-7.446861736264e-001 -6.674147906735e-001 0.000000000000e+000
6.674147906735e-001 -7.446861736264e-001 0.000000000000e+000
0.000000000000e+000 0.000000000000e+000 1.000000000000e+000
1.068276886824e-020 4.086601966469e-021 0.000000000000e+000
Die Parts 1 (47) müssen untereinander stehen, also sortiert werden.
Finde leider keinen ansatz.
MFG Murat Celik
Ähnliche Themen
-
CSV-Datei in PHP sortieren
Von Kalito im Forum PHPAntworten: 9Letzter Beitrag: 31.08.10, 16:19 -
Sortieren einer txt-Datei
Von touristguy im Forum C/C++Antworten: 2Letzter Beitrag: 14.07.08, 20:19 -
Externe Datei sortieren
Von MasterTobi im Forum C/C++Antworten: 0Letzter Beitrag: 28.11.07, 11:35 -
Datei auf HDD sortieren
Von DanielBodensee im Forum Delphi, Kylix, PascalAntworten: 1Letzter Beitrag: 19.04.04, 15:29 -
csv-datei sortieren
Von vomweg im Forum PHPAntworten: 6Letzter Beitrag: 21.07.03, 11:53





Zitieren
Login





