By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,805 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,805 IT Pros & Developers. It's quick & easy.

reordering elements of a list

P: n/a
I am trying to reorder elements of a list and I am stuck as to what
might be the best way to approach this. I have a (main) list of
elements and another (ordering) list (which is may shorter, but not
longer than the main list) which contains the order in which I want the
elements of the main list but only as far along as the length of the
ordering list. This may be confusing so I will try to give an example.

Suppose the main list is: mainlist = list('qwertyuiop')

Suppose the ordering list is: orderinglist = [3, 4, 2, 1]

Then I am looking for a function that will take mainlist and
orderinglist as arguments and return the following list:

['e', 'r', 'w', 'q', 't', 'y', 'u', 'i', 'o', 'p']

Also by the way the main list is always going to be a list of strings
and the ordering list will be a list of numbers. Also the largest
number in orderinglist will always be equal to the length of
orderinglist. I hope this makes any sense. Thanks for your help.

Jun 3 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
greenflame wrote:
I am trying to reorder elements of a list and I am stuck as to what
might be the best way to approach this. I have a (main) list of
elements and another (ordering) list (which is may shorter, but not
longer than the main list) which contains the order in which I want the
elements of the main list but only as far along as the length of the
ordering list. This may be confusing so I will try to give an example.

Suppose the main list is: mainlist = list('qwertyuiop')

Suppose the ordering list is: orderinglist = [3, 4, 2, 1]

Then I am looking for a function that will take mainlist and
orderinglist as arguments and return the following list:

['e', 'r', 'w', 'q', 't', 'y', 'u', 'i', 'o', 'p']


Here ya go, complete with example:

var aOrder = [8, 9, 0, 2, 18, 15, 27];
var aMain = [' '];
for (var i=0;i<26;++i) aMain.push(String.fromCharCode(65+i));
aMain.push('*'); // aMain is now [' ','A','B',...'Z','*']

reorder(aMain, aOrder);
alert (aMain);

function reorder(aMain, aSelected) {
// returns a reordering of aMain with the first
// aSelected.length items ordered according to aSelected
// and the rest in their original aMain order
var aPre=[], aSel=[]; //next line sorts to accomodate splice removals
for (var i=aSelected.length;i--;) aSel[aSelected[i]]=i+1;
for (i=aSel.length;i--;) if (aSel[i])
aPre[aSel[i]-1] = aMain.splice(i,1);
aMain.splice(0,0,aPre);
}
Csaba Gabor from Vienna

Jun 3 '06 #2

P: n/a
Thanks!

Csaba Gabor wrote:
greenflame wrote:
I am trying to reorder elements of a list and I am stuck as to what
might be the best way to approach this. I have a (main) list of
elements and another (ordering) list (which is may shorter, but not
longer than the main list) which contains the order in which I want the
elements of the main list but only as far along as the length of the
ordering list. This may be confusing so I will try to give an example.

Suppose the main list is: mainlist = list('qwertyuiop')

Suppose the ordering list is: orderinglist = [3, 4, 2, 1]

Then I am looking for a function that will take mainlist and
orderinglist as arguments and return the following list:

['e', 'r', 'w', 'q', 't', 'y', 'u', 'i', 'o', 'p']


Here ya go, complete with example:

var aOrder = [8, 9, 0, 2, 18, 15, 27];
var aMain = [' '];
for (var i=0;i<26;++i) aMain.push(String.fromCharCode(65+i));
aMain.push('*'); // aMain is now [' ','A','B',...'Z','*']

reorder(aMain, aOrder);
alert (aMain);

function reorder(aMain, aSelected) {
// returns a reordering of aMain with the first
// aSelected.length items ordered according to aSelected
// and the rest in their original aMain order
var aPre=[], aSel=[]; //next line sorts to accomodate splice removals
for (var i=aSelected.length;i--;) aSel[aSelected[i]]=i+1;
for (i=aSel.length;i--;) if (aSel[i])
aPre[aSel[i]-1] = aMain.splice(i,1);
aMain.splice(0,0,aPre);
}
Csaba Gabor from Vienna


Jun 4 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.