1

I have a code snippet that is not sorting correctly. I need to sort a HashMap by keys using TreeMap, then write out to a text file. I have looked online on sorting and found that TreeMap can sort a HashMap by keys but I am not sure if I am utilizing it incorrectly. Can someone please take a look at the code snippet and advise if this is incorrect?

public void compareDICOMSets() throws IOException
    {
        FileWriter fs;
        BufferedWriter bw;
        fs = new FileWriter("dicomTagResults.txt");
        bw = new BufferedWriter(fs);
        Map<String, String> sortedMap = new TreeMap<String, String>();
        for (Entry<String, String> entry : dicomFile.entrySet())
        {       
            String s = entry.toString().substring(0, Math.min(entry.toString().length(), 11));
            if(dicomTagList.containsKey(entry.getKey()))
            {
                sortedMap.put(s, entry.getValue());
                Result.put(s, entry.getValue());
                bw.write(s + entry.getValue() + "\r\n");                
            }
        }
        bw.close();
        menu.showMenu();
    }
}

UPDATE:

This is what I get for results when I do a println:

(0008,0080)
(0008,0092)
(0008,1010)
(0010,4000)
(0010,0010)
(0010,1030)
(0008,103E)
(0008,2111)
(0008,1050)
(0012,0040)
(0008,0094)
(0010,1001)

I am looking to sort this numerically. I have added String s to trim the Key down just to the tags as it was displaying a whole string of stuff that was unnecessary.

ryekayo
  • 2,341
  • 3
  • 23
  • 51
  • 1
    Already answered here http://stackoverflow.com/questions/922528/how-to-sort-map-values-by-key-in-java – abarisone Mar 18 '15 at 15:57
  • @abarisone, i took a look at this and I actually did refer to this before I asked the question. But i am almost convinced I am doing something wrong with my code. Care to take a look at it? – ryekayo Mar 18 '15 at 16:16
  • 1
    what is the problem in sorting? – rns Mar 18 '15 at 16:20
  • @rns, when I write it out to a text document, it is not sorted by the keys as it should be. – ryekayo Mar 18 '15 at 16:20
  • This code is very confusing to me. For every entry in the dicomFile map you create a new sortedMap containing all the Result entries, then add all Result entries again, and then write the key to bw. Then, you're not using the sortedMap for anything. Maybe you just want to add your entry to a single sortedMap, that will be sorted in the end, and print/write that one out? – Bram Mar 18 '15 at 17:02
  • Ok I moved the TreeMap outside of the the enhanced for and if statement so that it doesn't remake itself x amount of times. – ryekayo Mar 18 '15 at 17:12
  • So `Result` is an unsorted map? And what's `dicomFile` then? – fps Mar 18 '15 at 17:38
  • @Magnamag, dicomFile is a HashMap. Both are unsorted, when I try to use TreeMap to sort Result, It doesn't sort. – ryekayo Mar 18 '15 at 17:40
  • But where are the things you want to order? Are they in `Result` or in `dicomFile`? – fps Mar 18 '15 at 17:41
  • They are not initialized in snippet I have, they are initialized elsewhere in the code. Are you asking what the actual keys and values are? – ryekayo Mar 18 '15 at 17:42
  • I mean if what you want to order is within `Result` or within `dicomFile`? Which one of these 2 maps do you want to order? – fps Mar 18 '15 at 17:44
  • Can U post your Result class? ( Result.put(s, entry.getValue())) – rns Mar 18 '15 at 17:46
  • @rns I added the Result back in the code, it is in there. – ryekayo Mar 18 '15 at 17:49
  • @Magnamag, I want to order Result. – ryekayo Mar 18 '15 at 17:51

1 Answers1

2

You should first order your results, and then print them.

For Java 8:

Map<String, String> Result = ...;

// This orders your Result map by key, using String natural order
Map<String, String> ordered = new TreeMap<>(Result);

// Now write the results
BufferedWriter bw = new BufferedWriter(new FileWriter("dicomTagResults.txt"));
ordered.forEach((k, v) -> bw.write(k + v + "\r\n");

bw.close();

For pre Java 8:

Map<String, String> Result = ...;

// This orders your Result map by key, using String natural order
Map<String, String> ordered = new TreeMap<>(Result);

// Now write the results
BufferedWriter bw = new BufferedWriter(new FileWriter("dicomTagResults.txt"));
for (Map.Entry<String, String> entry : ordered.entrySet()) {
    String k = entry.getKey();
    String v = entry.getValue();
    bw.write(k + v + "\r\n");
}

bw.close();
fps
  • 33,623
  • 8
  • 55
  • 110