4

ユーザーが追加しているいくつかの URL のサフィックスを格納するこの配列があります。

[U2, U3, U1, U5, U8, U4, U7, U6]

私がこれを行うとき:

for (Map<String, String> map : getUrlAttachments()) {
            String tmpId = map.get("id"); //it receives the U2, in the 1st iteration, then U3, then U1,...
            if (tmpId.charAt(0) == 'U') {
                tmpId.charAt(1);//2, then 3, then 1,...
                String url = map.get("url");
                String description = map.get("description");
                URLAttachment attachment;
                String cleanup = map.get("cleanup");
                if (cleanup == null && url != null && description != null) {
                    attachment = new URLAttachmentImpl();
                    attachment.setOwnerClass(FileUploadOwnerClass.Event.toString());
                    attachment.setUrl(url);
                    attachment.setDescription(description);
                    attachment.setOwnerId(auctionHeaderID);
                    attachment.setUrlAttachmentType(URLAttachmentTypeEnum.EVENT_ATTACHMENT);
                    attachment.setDateAdded(new Date());
                    urlBPO.save(attachment);

            }

私の問題:

Forのようにソートされたデータをマッピングする別のリストを渡すことで、この条件を変更したいと思い[U1, U2, U3, U4, U5, U6, U7, U8]ます。

これを行う最善の方法を教えてください。

IDをリストした配列を作成してから並べ替えることを考えましたが、Javaで英数字の文字列を正確に並べ替える方法がわかりません。

4

4 に答える 4

3

@Abu が提供したアイデアを使用することにしましたが、それを適応させました。

  1. ユーザーが追加しようとしている URL の ID を確認します。
  2. この ID のアルファベットの接尾辞を削除し、ArrayList各 ID の数値部分を格納する を作成します。
  3. ArrayList@Abuが彼の答えで教えてくれたようにこれをソートArrayListし、追加する必要があるシーケンスでソートされたこのソートの各IDを検証します..

    ArrayList <Integer> urlSorted = new ArrayList<Integer>();
    //sort the url ids
    for (Map<String, String> map : getUrlAttachments()) {
        String tmpId = map.get("id");
        if (tmpId.charAt(0) == 'U') {
            //gets the id, removing the prefix 'U'
            urlSorted.add( Integer.valueOf(tmpId.substring(1)));
        }
    }
    //sort the urlIds to check the sequence they must be added
    Collections.sort(urlSorted);
    //checks for each url id, compares if it's on the natural order of sorting to be added.
    for(Integer urlId: urlSorted) {
        for (Map<String, String> map : getUrlAttachments()) {
            String sortedId = "U"+urlId;
            String tmpId = map.get("id");
            //compare the ids to add the 1, then 2, then 3...
            if (map.get("id").equals(sortedId)) {
                        //code to save according to the sorted ids.
            }
         }
    }
    
于 2013-01-17T15:32:50.010 に答える
3

次のように値Collections.sort()を作成した後、メソッドを使用するだけです。ArrayList

ArrayList<String> a = new ArrayList<String>();
a.add("U2");
a.add("U1");
a.add("U5");
a.add("U4");
a.add("U3");
System.out.println("Before : "+a);
Collections.sort(a);
System.out.println("After : "+a);

出力:

Before : [U2, U1, U5, U4, U3]
After : [U1, U2, U3, U4, U5]
于 2013-01-15T12:28:44.323 に答える
1

カスタムを作成しますComparator<Map<String,String>>

public class IdComparator implements Comparator<Map<String,String>> {
  public int compare(Map<String,String> left, Map<String,String> right) {
    return orderKey(left).compareTo(orderKey(right));
  }
  static Integer orderKey(Map<String,String> m) { 
    return Integer.parseInt(m.get("id").substring(1)); 
  }
}

そして、それArrays.sort(urlAttachments, new IdComparator());を反復する前に使用します。詳細に応じて、この並べ替えロジックをプッシュしgetUrlAttachments()て、投稿したコードをそのまま保持することができます。

于 2013-01-15T12:29:22.423 に答える