-4

I have a map, whose keys I am passing into an ArrayList but as map in unordered that's why even the ArrayLists output is unordered.

I used Collections.sort(keys) but it is ordering keys only upto number 10. How to order it after 10 I am not understanding.

The keys look like this:

PartOrderDateRaised_mva_p_2

PartOrderDateRaised_mva_p_3

Set<String> keys = paramMap.keySet();
    for (String key : keys){ 
    if (key.contains("iPLMPartOrderDateRaised_mva")) {

            String partOrderDateValue = (String) paramMap.get(key);
            strPartOrderDate += partOrderDateValue + "~";
        }
    }

Note - paramMap contains all keys which I am trying to take through Set, but it not worked. So I tried through ArrayList and then realised that arraylist gives the o/p, the way it takes i/p(Param map content in my case which is unordered).

List<String> keys = new ArrayList<String>(paramMap.keySet());

Collections.sort (keys);

for (String key : keys) { 

if (key.contains("iPLMPartOrderDateRaised_mva")) {
        String partOrderDateValue = (String) paramMap.get(key);
        strPartOrderDate += partOrderDateValue + "~";

    }
}
Arun Xavier
  • 763
  • 8
  • 47
Pranav
  • 3
  • 2
  • 2
    post your code please. – Arun Xavier Feb 01 '16 at 09:47
  • paramMap contains all keys which I am trying to take through Set, but it not worked. So I tried through ArrayList and then realised that arraylist gives the o/p, the way it takes i/p(Param map content in my case which is unordered). – Pranav Feb 01 '16 at 10:10
  • Hi , your key is a String , So sorting will be working as per String sorting. eg: sorting Strings 1,2,21,12,10 will give 1,10,12 ,2 ,21. you should consider adding a custom key class for custom sorting. – Arun Xavier Feb 01 '16 at 10:24
  • and please try to edit your question by adding the code you pasted in comments. – Arun Xavier Feb 01 '16 at 10:25
  • I added the code in my question, being new to portal I am not aware about posting things. Please can you elaborate or give an example for handling such strings. – Pranav Feb 01 '16 at 10:48
  • http://stackoverflow.com/editing-help & http://stackoverflow.com/help/how-to-ask – Arun Xavier Feb 01 '16 at 10:49
  • 1
    http://stackoverflow.com/questions/1206073/sorting-a-collection-of-objects , http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property – Arun Xavier Feb 01 '16 at 11:12

1 Answers1

0

I am adding a simple comparison here based on my understanding of your question. You can modify your code based on that.

package test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class Test {
    public static void main(String[] args) {
        Map<CustomKey,String> map = new HashMap<>();

        CustomKey ck = new CustomKey(1, "PartOrderDateRaised_mva_p_");
        map.put(ck, "hello");
        CustomKey ckOne = new CustomKey(15, "PartOrderDateRaised_mva_p_");
        map.put(ckOne, "hello");
        CustomKey ckTwo = new CustomKey(10, "PartOrderDateRaised_mva_p_");
        map.put(ckTwo, "hello");
        CustomKey ckThree = new CustomKey(3, "PartOrderDateRaised_mva_p_");
        map.put(ckThree, "hello");
        CustomKey ckFour = new CustomKey(5, "PartOrderDateRaised_mva_p_");
        map.put(ckFour, "hello");
        CustomKey ckFive = new CustomKey(11, "PartOrderDateRaised_mva_p_");
        map.put(ckFive, "hello");

        ArrayList<CustomKey> keys = new ArrayList<>(map.keySet());
        Collections.sort(keys);
        for (CustomKey key : keys) {
        System.out.println(key);
    }
    }


}
class CustomKey implements Comparable<CustomKey>{
    Integer codeNumber;
    String codeText;
    public CustomKey(Integer codeNumber, String codeText) {
        this.codeNumber = codeNumber;
        this.codeText = codeText;
    }
    public Integer getCodeNumber() {
        return codeNumber;
    }
    public void setCodeNumber(Integer codeNumber) {
        this.codeNumber = codeNumber;
    }
    public String getCodeText() {
        return codeText;
    }
    public void setCodeText(String codeText) {
        this.codeText = codeText;
    }
    @Override
    public String toString() {
        return this.getCodeText()+this.getCodeNumber(); 
    }
    @Override
    public int compareTo(CustomKey o) {

        return this.getCodeNumber().compareTo(o.getCodeNumber());
    }
}

Here CustomKey class implements Comparable and overrides it's method compareTo.

Output

PartOrderDateRaised_mva_p_1
PartOrderDateRaised_mva_p_3
PartOrderDateRaised_mva_p_5
PartOrderDateRaised_mva_p_10
PartOrderDateRaised_mva_p_11
PartOrderDateRaised_mva_p_15

If you are trying to achieve something different , please add your input and expected output in your question.

Arun Xavier
  • 763
  • 8
  • 47