ArrayList 的自定义排序

我想根据我的逻辑对 ArrayList 进行排序:

        ArrayList<String> cards = new ArrayList<>(Arrays.asList("K","A","7","Q","6","J","T"));
        // desired result: [6, 7, T, J, Q, K, A]

所以就像扑克牌一样,这是升序:2、3、4、5、6、7、8、9、T、J、Q、K、A

我怎么能做到呢?


子衿沉夜
浏览 187回答 2
2回答

缥缈止盈

请尝试此代码是否对您有帮助。import java.util.*;class CustomComparator implements Comparator<String> {&nbsp; &nbsp; final String ORDER= "23456789TJQKA";&nbsp; &nbsp; public int compare(String a, String b) {&nbsp; &nbsp; &nbsp; &nbsp; return ORDER.indexOf(a) -&nbsp; ORDER.indexOf(b) ;&nbsp; &nbsp; }}public class SortIt {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; List cards = new ArrayList<>(Arrays.asList("K", "A", "7", "Q", "6", "J", "T"));&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(cards, new CustomComparator());&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(cards);&nbsp; &nbsp; }}输出:[6, 7, T, J, Q, K, A]

三国纷争

如果我是你,我会使用它。这只是另一个答案,如果复杂性对您来说并不重要,您可以使用它。也许你对你的其他项目有一个想法。public List<String> sort(String[] cards) {&nbsp; &nbsp; for (int i = 0; i < cards.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; switch (cards[i]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "T":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards[i] = "91";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "J":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards[i] = "92";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "Q":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards[i] = "93";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "K":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards[i] = "94";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "A":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards[i] = "95";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; Arrays.sort(cards);&nbsp; &nbsp; for (int i = 0; i < cards.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; switch (cards[i]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "91":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards[i] = "T";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "92":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards[i] = "J";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "93":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards[i] = "Q";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "94":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards[i] = "K";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "95":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards[i] = "A";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return Arrays.asList(cards);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java