PDA

View Full Version : Arranging values.


ashleypower
03-10-2003, 07:34 PM
Ok, I have 6 numbers in 6 fields named from "a" to "f".

I need a script that finds the top 3 numbers (highest to lowest numerical order) of the 6 fields, and puts them in order of their letters. Here's what I mean:

Let's say these are the values of the fields:

A = 1
B = 5
C = 8
D = 22
E = 3
F = 13

The page would output this:

D F C

If two numbers are equal, it then puts them in alphabetical order:

A = 1
B = 7
C = 19
D = 32
E = 19
F = 2

would output this:

D C E (if two values are equal, takes them in alphabetical order)

Any help would be greatly appreciated!

beetle
03-10-2003, 07:55 PM
outputs them how? Into a textarea in the form? Another input? written to the page? Submitted? Alerted?

ashleypower
03-10-2003, 10:37 PM
Well, I would rather if this three letter code wasn't seen by the user. Ultimately, this 3 letter code will eventually search a database and return information based on the three letter code. Maybe into a hidden field? I'm not sure if it's possible, but maybe a variable? I'm not sure how ASP works with variables, but what would be your suggestion?

joh6nn
03-11-2003, 01:19 AM
you have to come up with a way to keep the number associated with its letter. i came up with the following.

var theArray = ["1A", "5B", "8C", "22D", "3E", "13F"];
theArray.sort().reverse();
var theString = theArray[0].charAt(theArray[0].length-1) + theArray[1].charAt(theArray[1].length-1) + theArray[2].charAt(theArray[2].length-1);

that doesn't check for duplicate numbers, but adding that shouldn't be too much trouble

ashleypower
03-11-2003, 03:57 AM
I see that you've used an array...the numbers in the 6 fields aren't going to be the same always...they will change, meaning that the letters will be in different order. If I use an array, wouldn't the numbers have to be the same always?

beetle
03-11-2003, 05:01 AM
Here's an associative array (read: object) sorting method I wrote a while ago...should work just peachy for this.Object.prototype.assocSort = function( kv, desc )
{
// Init Vars
var a = new Array();
var i = 0;

// Populate array with objects
for ( var k in this )
{
if ( this.invalidProp( k ) ) continue;
a[i++] = {key: k, value: this[k]};
}

// Sort Array
a.sort( sortHandler );

// Create new object and populate properties in desired order
var o = new Object();
for ( var i = 0; ( x = a[i] ); i++ )
o[x.key] = x.value;

// return new object
return o;

// Sort by key or value
function sortHandler( a, b )
{
if ( a[kv] > b[kv] ) return ( desc ) ? -1 : 1;
if ( a[kv] < b[kv] ) return ( desc ) ? 1 : -1;
return 0;
}
}

Object.prototype.invalidProp = function( k )
{
// Return true if property is not a String or Number
return ( !/(?:string)|(?:number)/i.test( typeof this[k] ) );
}Usagevar data = {'A':1, 'B':5,'C':8,'D':22,'E':3,'F':13};
data = data.assocSort( 'value', 1 ); // Sort by value, descending

joh6nn
03-11-2003, 05:20 AM
no, the numbers can still change in the array. i did some quick testing before i posted the code, to make sure that the array's sort method would still provide the desired results, even with a mix of letters and numbers, and in my (fairly limited) trials, the code that i posted always gave the appropriate output.

made a slight mistake in my first post (charAt, not char), it's fixed now.