选择排序的字符串数组(JAVA) [英] Selection Sorting String Arrays (Java)

查看:108
本文介绍了选择排序的字符串数组(JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何排序字符串数组,我知道该怎么做一个选择排序,但是我有数组中的数字和字母。我们的想法是要排序的牌手。
这是我有...

I need to know how to sort an array of Strings, I know how to do a selection sort, however I have BOTH numbers and letters in the array. The idea is to sort a hand of cards. This is what I have ...

clubsArry[0] = 7C

clubsArry[1] = AC

clubsArry[2] = TC

clubsArry[3] = KC

第二个字符再presents卡的套装,在这种情况下,俱乐部和第一个字符是卡值(此时T重新presents十,K为王,A是王牌等等。 ),因此对于clubsArry [0],该卡是俱乐部的7。当我排序并打印,输出我得到的是....

The second character represents the suit of the card, in this case clubs, and the first character is the card value (the T represents Ten, K is king, A is ace, etc.), so for the clubsArry[0] , the card is a 7 of clubs. When I sort it and print, the output I get is ....

7C

AC

KC

TC

所以,所有那些在第一位置的字母卡得到正确排序,但我仍然有俱乐部的7是在输出的顶部。我想输出是...

So all the cards that that have a letter in the first position get sorted properly but I still have the 7 of clubs that is at the top of the output. The output that I want is ...

AC

KC

TC

7C

我试图对它进行排序的方法就是这样:

The way I tried to sort it was by doing this:

public void sortClubs() // A selection sort, that will sort out the clubs 
{
    for (int i = 1; i < clubsArry.length; i++)
    {
        int s = i-1;
        for (int j = i; j < clubsArry.length; j++)
        {
            if (clubsArry[j].compareTo(clubsArry[s]) < 0)
            {
                 s = j;
            }
        }
         String temp = clubsArry[i-1];
         clubsArry[i-1] = clubsArry[s];
         clubsArry[s] = temp;
    }
}

我只是不能似乎数字出来​​,我要去把它转换成一个字符数组,然后浇铸成一个int和排序,并将其转换都回来了,但我想一定有做这个简单的方法,如果有一种方法都没有。

I just cant seem to figure it out, I was going to convert it into a char array and then cast into an int and the sort and convert it all back, but I figure there must be a simpler way to do this, If there is a way at all.

感谢所有帮助:)

MadcapClover

MadcapClover

推荐答案

使用比较那么 Arrays.sort()方法。这应该与你的编码帮助,因为它可以让你专注于一件事:如何订购2个不同的卡?你将不得不硬code的顺序弄好了,这里是做到这一点的方法之一。我没有测试过它;它仅排序基于所述第一个字符。如果第一个字符是相同的,那么你应该看看第二个系统字符。

Use a Comparator, then the Arrays.sort() method. This should help with your coding as it lets you focus on a single thing: how do I order 2 different cards? You will have to hard-code in the ordering somehow, here is one way to do it. I haven't tested it; and it only sorts based on the first character. If the first characters are the same, then you should look at the second charater.

import java.util.Comparator;

public class CardComparator implements Comparator<String> {

    private static final List<Character> CARD_ORDER = Arrays.asList( 
    'A', 'K', 'Q', 'J', '1', '9', '8', '7', '6', '5', '4', '3', '2');

    @Override
    public int compare(String card1, String card2) {
        if( card1.equals(card2)) {
            return 0;
        }

        int firstIndex = CARD_ORDER.indexOf(card1.charAt(0));
        int secondIndex = CARD_ORDER.indexOf(card2.charAt(0));

        return firstIndex - secondIndex;
    }
}

这篇关于选择排序的字符串数组(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆