Results 1 to 4 of 4
Thread: Sort Logic
- 02-03-2012, 09:48 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 31
- Rep Power
- 0
Sort Logic
This isn't exactly specific to Java, but more asking for advice on the logic of a sort I'm trying to implement.
I have a list of objects that have been compared to each other. There are "Winners" and "Losers".
So my list might be something like:
A beats B
B beats C
B beats D
D beats C
E beats A
And I want to sort them, so the "best" are at the top, so in that example the list would be sorted to:
E, A, B, D, C
I'm unsure how to tackle this logically. I've been thinking about every time I encounter a new element, looking up all the "rules" it's a part of, and then adding it to my "sorted" array based on those rules and the elements that are already in there.
I can't help feel there must be a better way and even with the above method I'm not sure how I would program it. Any advice would be awesome. Cheers.
- 02-03-2012, 10:22 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Re: Sort Logic
Your problem can not be solved in general; e.g.
A beats B
B beats C
C beats A
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-03-2012, 11:01 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 31
- Rep Power
- 0
Re: Sort Logic
I want to try and add validation to the input to try and stop this from happening, so for now I'm assuming that this conflict won't happen, or I'll implement some kind of conflict resolution when it encounters this.
- 02-03-2012, 12:07 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Re: Sort Logic
A precedence matrix might come in handy then; create a square matrix of booleans; the rows and columns are labeled with the names of the competitors; if 'i' beats 'j' set entry M[ i ][ j ] to true; after you have done this for all competitors, try to rearrange the rows such that no entry M[ i ][ j ] is true for i >= j. If you can do that, there is no circularity in the accompanying graph and the relation 'beats' is a PTO (Partial Total Ordering) and you can find all the information in matrix M.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Where's the logic?
By diamonddragon in forum New To JavaReplies: 11Last Post: 01-28-2012, 04:16 AM -
need a logic for this
By rajivjoshi in forum New To JavaReplies: 4Last Post: 06-12-2010, 02:18 PM -
Using Merge Sort to sort an ArrayList of Strings
By coldfire in forum New To JavaReplies: 3Last Post: 03-13-2009, 01:03 AM -
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM -
Cant get the logic right
By jermaindefoe in forum New To JavaReplies: 4Last Post: 03-11-2008, 12:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks