Hi everyone! I have been trying to decide how to do this for quite some time and I finally decided I just need some assistance. I have an array of Comparable objects and I want to sort them, but I want to do it in a weird way. I'd like to sort from highest to lowest, but also place repeated entries at the front of the array. The more times the appear the more priority they get so something like

1

unsorted = {7, 2, 3, 2, 1, 5}

will get sorted as

1

sorted = {2, 2, 7, 5, 3, 1}

Where the 2s get put at the front because they appear twice. Here is another example:

1

unsorted = {1, 2, 3, 2, 4, 2, 3, 5, 3, 5}

will look like

1

sorted = {3, 3, 3, 2, 2, 2, 5, 5, 4, 1}

where you can see that there are three 3s and three 2s, so the 3s appear first in the sorted array. 5 appears after this because there are only two of them. Lastly we get 4 and 1.

The way to make this array is to go through the unsorted array many times counting...pseudo code made in 1 minute without testing:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

sortArray[0][0] = unsorted[0] //here I store in [0][0] the first value. In this case, the number 1inth = HowManyDifferent(unsorted); // method to count how many different numbers there are in an arrayintk=0;intg=0;while (g<h){for (k=0; k<unsorted.size();k++){if (unsorted[k] == sortArray[i][0]){sortArray[i][1]++; //here I count how many 1's there are in unsorted } }k=0;i++;if (! repeated){sortArray[i][0] = unsorted[next] }}

It's very ugly, but you get the idea.Once you have sortArray filled, it's easy. Just order it by "j" (the frequency), then order it by "i" taking in consideration "j":

I would use linked sorted sets, this should guarantee around constant adding and removing from the sorted list

Create a node class which holds a some sorted set class and has an empty next node.when you want to add a new nummber take a look if the head node has this nummber already in its set, if not insert it,if add it to the heads next node(if none exist creat a new one)

removing works the excat opposit, try to remove it from the tail and work you towards he head node.

Thinking logically about this:-sort list from smallest to largest-remove and add repeated entries in an ArrayList of ArrayLists.-push remaining items onto a stack-sort ArrayList of ArrayLists into ascending order by comparing lengths or value-push the Arraylists into the stack-popping the stack will give you the desired sorted list

I might not have explained it well enough so if you need code, just ask

public <TextendsComparable<? superT>> voidweirdSort(ArrayList<T> list) {//sort from least to greatestCollections.sort(list);

ArrayList<ArrayList<T>> repeats = newArrayList<ArrayList<T>>();

for(inta = 0; a < list.size(); a++) {Tt = list.get(a);

intsize = repeats.size();

//if 't' equals the one after it and either the repeats is empty or that value hasn't been added before//then create a new list, and add itif((size == 0 || !repeats.get(size-1).get(0).equals(t)) && (a < list.size()-1 && t.equals(list.get(a+1)))) {repeats.add(newArrayList<T>());repeats.get(size).add(list.remove(a));a--; }//if 't' is a repeat, add it to the repeated listelseif(size != 0 && repeats.get(size-1).get(0).equals(t)) {repeats.get(size-1).add(list.remove(a));a--; } }

The real question is, what things do you really want to do? Do you really want to sort something and that's it or what? There are tons of different ways this could be structured, but what make sense depends how what exactly is needed and what operations are going to happen the most if more than one is desired. Your ordering can be achieved by thinking in terms of set. So input {1,1,1} -> {{3,1}} (i.e. 3 1's) and {1,2,3} -> {{1,1},{1,2},{1,3}} (I'm showing reversed of your desired order, but that doesn't matter), etc. Since your number of ranks is small, this set info can be packed into a single integer as follows:

if (num != 0) {// do whatever real work here.// * for example if you really want sorted, you// know how much space in the output each will// take and can directly fill an output structure// yielding an O(n) (linear time) sorting.

i--;continue; }

break; }

// *** BELOW HERE HACKY DEMO CODE:

int[] o = newint[a.length];

// dump out input set in original orderSystem.out.printf("in: {");for(i=0;i<a.length-1;i++)System.out.printf("%d,", a[i]);System.out.printf("%d}\n",a[i]);

which is not what I want. I am looking over the code now to see if I can find a problem, but thought I'd let you know that there is an issue. If I find a solution I will post it below.

Quote

The real question is, what things do you really want to do? Do you really want to sort something and that's it or what?

This is for a card game I am inventing. The purpose of this sorting thing is to find out who has the best hand, and to also print the cards out in a logical looking order. So the sort is happening on these cards. Now each card has a rank and a picture, where we have that

1 2 3

publicintcompareTo(Cardc) {returnthis.rank - c.getRank();}

so the ordering is not consistent with Object.equals(). I know it says here: (http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html) not to do this, but whatever! Basically, for this sorting I don't care if the cards have different pictures or not. But because of this I can't just store ordered pairs like (3, 1) for "three ones" because the ones may have different pictures and I don't want to lose that information.[/code]

I would:1. identify valid card combinations for each hand, define classes for two-of-a-kind, three-of-a-kind, full-house, etc.2. put a list together of those for each hand3. sort the combination lists of each player by implementing proper Comparators

-> easy to modify or extend rules-> operates on a more abstract level instead of sorting cards directly

Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.

I would:1. identify valid card combinations for each hand, define classes for two-of-a-kind, three-of-a-kind, full-house, etc.2. put a list together of those for each hand3. sort the combination lists of each player by implementing proper Comparators

-> easy to modify or extend rules-> operates on a more abstract level instead of sorting cards directly

That's not a bad idea except that hands can be an arbitrary number of cards. In Poker, where you only have 5 cards hands there are a small number of possible combinations, but they grow like e^(sqrt(n))/n which grows pretty fast (see http://en.wikipedia.org/wiki/Partition_(number_theory) for math about this ) While there is an upper limit to the size of a hand, the number of possible hands is too large for me to individually make a class for each one.

I would have bet you are developing a Poker game Anyway, I did not mean one class for each card combination but for each "value class" or however its called.So, for Poker there would be one class for the Royal Flush, one for Full House, one for Straight, etc.Objects of these value classes would get attributes for current cards of a hand.

Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org