Array.Sort Logic Help, Bill W...?

# Thread: Array.Sort Logic Help, Bill W...?

1. Member
Join Date
Dec 1969
Posts
38

## Array.Sort Logic Help, Bill W...?

Hi all..<BR><BR>I am having some trouble with some logic in my array.sort function. The function was actually written by Bill W. in a response to an older thread...<BR><BR>Basically, cell [1] is always a positive number, and that part works like a charm, the higher number gets sorted properly up one.<BR><BR>The second part the cell [5] contains either a positive, negative or zero. Basically this is the difference between 2 numbers, so it would be positive if the number is higher, etc...<BR><BR>What I am trying to accomplish in this part is a &#039;Closest without going over&#039; type condition, and I added the code line if ( e2[5] &#062;= 0 ) return 1; to try and accomplish it. No Luck..<BR><BR>Can someone please help me? And thanks in advance for reading this long post.<BR><BR>b<BR><BR><BR> // compare first values<BR> if ( e1[1] &#060; e2[1] ) return 1; <BR> if ( e1[1] &#062; e2[1] ) return -1; <BR> // values are same, compare on last numbers <BR> if ( e2[5] &#062;= 0 ) return 1;<BR> if ( e1[5] &#060; e2[5] ) return -1; <BR> if ( e1[5] &#062; e2[5] ) return 1; <BR> // truly equal <BR> return 0; <BR> }//end function<BR>

2. Member
Join Date
Dec 1969
Posts
38

## RE: Array.Sort Logic Help, Bill W...?

I couldn&#039;t find any edit button for the post, but after I read it, I wanted to mention that the function did do exactly what I had asked for help on before, I didn&#039;t want to seem like I hadnt received fantastic help from Bill in the previous post..<BR>b

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## I don't get it...

I don&#039;t understand why whether cell[5] is positive or negative (or zero) makes any difference.<BR><BR>-1 &#060; 7 is still true.<BR><BR>-3 &#060; -1 is still true.<BR><BR>0 == 0 is still true. <BR><BR>etc.<BR><BR>Can you give some example data, showing it unsorted and sorted, in the order you want? Just show the cell[1] and cell[5] values for, say, six or eight cells.<BR><BR>

4. Member
Join Date
Dec 1969
Posts
38

## RE: I don't get it...

Bill, Sure, let me try to explain...<BR><BR>Here is some sample data cell[1] , cell[5]:<BR><BR>19, -42<BR>19, -1<BR>19, 3<BR>19, 8<BR>19, 0<BR>9, -2<BR>10, 3<BR>10,-1<BR><BR>I need the sort to return it in this order:<BR>19,0<BR>19,-1,<BR>19,-42<BR>19, 3<BR>19, 8<BR>10,-1<BR>10,3<BR>9,-2<BR><BR>The problem I am having is trying to get the logic straight. So, basicall, ignoring the firs cell sort[1], since it is a a straightforward greater than or less than situation.<BR><BR>The second one, I need it to sort so that zero is higher than anything except another zero (user has guessed the number correctly). If it is less than zero, than the number closest to zero would be next (closest without going over), and if the number is greater than zero, than it should sort in ascending order...<BR><BR>Make any more sense? It is tough to explain, but think if I asked everyone in the room to guess the number I am thinking of, then I put them into an array and had to sort it. The sort needs to be centered around zero ...<BR><BR>Thanks again for the help, this is frying my noodle...<BR><BR>b<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## And still again...

19,0<BR>19,-1,<BR>19,-42<BR>19, 3<BR>19, 8<BR><BR><BR>Why???<BR><BR>-42 is a lot further from zero than 3 or 8.<BR><BR>Why isn&#039;t it<BR><BR>19,0<BR>19,-1,<BR>19, 3<BR>19, 8<BR>19,-42<BR><BR>????<BR><BR>

6. Member
Join Date
Dec 1969
Posts
38

## RE: And still again...

Because if I am guessing a number and the winner is the closest without going over, then 3 over the number should be sorted lower in the list than 42 (-42) under the number.<BR><BR>My values are the difference between my guess and the actual number.<BR><BR>Thanks again for the assist, I can&#039;t think of any other way to explain it.<BR><BR>b

7. Member
Join Date
Dec 1969
Posts
38

## RE: And still again...

I think I have it working now, at least it seems to test okay under all conditions so far...<BR><BR>function MyCompare( e1, e2 ) {<BR> // compare first values<BR> if ( e1[1] &#060; e2[1] ) return 1; <BR> if ( e1[1] &#062; e2[1] ) return -1; <BR> // values are same, compare on last numbers <BR> if ( e1[5] &#062; 0 ) return 1;<BR> if ( e1[5] &#060; e2[5] ) return 1; <BR> if ( e1[5] &#062; e2[5] ) return -1; <BR> // truly equal <BR> return 0; <BR> }//end function<BR><BR>See anything funky?<BR><BR>thanks,<BR><BR>b

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## Almost there...

You are going to return 1 for two postitive numbers, no matter what. Wouldn&#039;t it be better to return a real comparison result?<BR><BR>MyCompare( e1, e2 ) {<BR> // compare first values<BR> if ( e1[1] &#060; e2[1] ) return 1; <BR> if ( e1[1] &#062; e2[1] ) return -1; <BR> // values are same, compare on last numbers <BR> if ( e1[5] == e2[5] ) return 0; // if equal, always zero<BR> if ( e1[5] &#062;= 0 ) <BR> {<BR> // for positive numbers the more positive one is the higher ranked<BR> // (where higher ranked means worse, of course)<BR> // note that if e2[5] is negative, we&#039;ll always <BR> // make e1 the higher ranked with this comparison:<BR> return ( e1[5] &#062; e2[5] ) ? 1 : -1;<BR> } <BR> // if here, e1 is negative, sense is reversed<BR> return ( e1[5] &#062; e2[5] ) ? -1 : 1;<BR>}<BR><BR>Does this work? Please test it extensively!<BR><BR>

9. Member
Join Date
Dec 1969
Posts
38

## RE: Not yet......

Bill,<BR><BR>No Luck. Since the first part works without a problem, I am just going to list the last numbers cell[5] that are being compared.<BR><BR>Here is the order that is being returned from the function I am replying to :<BR><BR>0<BR>+2<BR>-77<BR><BR>The order should be<BR>0<BR>-77<BR>+2<BR><BR>Since the zero is a correct guess, it always wins, since the +2 is over, it is lower than the -77 guess..<BR><BR>Thanks once again for the help, this is turning out to be more difficult than I envisioned...<BR><BR>Any thoughts?<BR><BR>b

10. Member
Join Date
Dec 1969
Posts
38

## RE: Going Crazy....

Bill,<BR><BR>I have been staring at the function you just posted for hours, and I cannot figure out why the +2 is sorting above the -77... The logic seems perfect...<BR><BR>Arrrgh. Help.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•