문제

I need your help with following issue:

In my code I have a list of elements, I need to sort this list according to 2 attributes: season and number.

List example:

<episode_list>
<episode id="280" number="13" season="1">
<title><![CDATA[Bowl Game]]></title>
</episode>
<episode id="314" number="12" season="1">
<title><![CDATA[Piss Test]]></title>
</episode>
<episode id="730" number="11" season="1">

I use Collections.sort(), but getting exception. As I understand I can't use it with JDOM Elements:

List<Element> episodes;

Collections.sort(episodes, new Comparator<Element>() {

            @Override
            public int compare(Element elem1, Element elem2) {
                Integer seasonNumber1 = Integer.valueOf(myService.valueOfAttribute("season", elem1));
                Integer seasonNumber2 = Integer.valueOf(myService.valueOfAttribute("season", elem2));
                int seasonComp = seasonNumber1.compareTo(seasonNumber2);
                if (seasonComp != 0) {
                    return seasonComp;
                } else {
                    Integer episodeNumber1 = Integer.valueOf(myService.valueOfAttribute("number", elem1));
                    Integer episodeNumber2 = Integer.valueOf(myService.valueOfAttribute("number", elem2));
                    return episodeNumber1.compareTo(episodeNumber2);
                }                  
            }      
        });


Exception: java.util.Collections$UnmodifiableList$1.set(Unknown Source)
          java.util.Collections.sort(Unknown Source)

Actually I don't need sorted xml, the only thing I need is the episode attribute "id" (for the lowest season and the lowest episode number).

What could you recommend? I have another implementation, where I go through all elements, but I don't think it's a nice solution...I also can create Java class Episode(id, episode, season), transform List to List and sort it, but also don't think it's a good idea. There is also sortContent method for Element, but I'm not sure how to implement it.

I'll appreciate any help.

도움이 되었습니까?

해결책 2

What's wrong with going through the list and finding the minimum. It is O(n), while sorting is O(n*log(n)). You might use a generic min function, such as the one in guava

Element firstEpisode = Ordering.from(your-comparator).min(episodes.iterator());

If you really want to sort it, why don't you sort new ArrayList<Element>(episodes) (I agree with rolfl that you cannot use Collections.sort for JDOM lists and that the error comes from your use of unmodifiable list).

다른 팁

Content attached to JDOM Elements cannot be sorted using the standard Collections.sort() mechanism because that process does not honour the only-attached-at-one-place-at-a-time rule for XML content.

JDOM has sort() methods built in to the Element class that allows you to sort the chile Elements or other Child content: See The Element.sortChildren() Javadoc for the JDOM way to do it.

Update: Also, for your reference, the error you are getting is because at some point you created an unmodifiable version of the List.... this is not something that happens from JDOM method calls. The error you are getting is because you are trying to modify a List that has been intentionally made read-only.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top