Link to home
Start Free TrialLog in
Avatar of sargento
sargento

asked on

Sorting objects within objects?

What I want to do is make multiple objects (will vary in number) and sort them but I need to identify and do a sub sorting of some of the objects. For example if I create these objects:

function Object(navName,linkPath,navPos) {
    this.navName           = navName;
    this.linkPath              = navLinkPath;
    this.navPos           = navPos;
   
}

function setObject(navName,linkPath,navPos) {
    myObjectArray[objectArrayIndex++] = new Object(navName,navLinkPath,navPos);
}

var objectArrayIndex = 0;
var myObjectArray = new Array();


setObject("cars","cars/",3);
setObject("dogs","dogs/",2);
setObject("cats","cats/",1);
setObject("sports","sports/",4);
setObject("foods","foods/",5);
setObject("doberman","dogs/doberman",2);
setObject("fishing","sports/fishing",1);
setObject("collie","dogs/collie",1);
setObject("track","sports/track/",2);
setObject("boxing","sports/boxing/",3);


I would want to sort the output to sort by:
FIRST the objects that only have the pattern xxxx/ as the linkPath (alphabetically or by navPos) and then sort sub associations that have the pattern xxxx/xxxx/ as the linkPath grouped by linkPaths.

So the above example could look like this if sorted by navPos:
"cats","cats/",1;
"dogs","dogs/",2;
"collie","dogs/collie",1;
"doberman","dogs/doberman",2;
"cars","cars/",3;
"sports","sports/",4;
"fishing","sports/fishing",1;
"track","sports/track/",2;
"boxing","sports/boxing/",3;
"foods","foods/",5;

Remember the objects could be created in any order and I need to be able to perform alphabetical or position sorting on them.


Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Try playing with this:

function myObjectToString() {
  slashPos = this.navLinkPath.indexOf('/');
  if (slashPos!=-1)
     return this.navLinkPath.substring(0,slashPos) + this.navLinkPath;
  else return this.navLinkPath;

}

myObjectArray.prototype.toString = myObjectToString;


Michel
 
ASKER CERTIFIED SOLUTION
Avatar of dmethvin
dmethvin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial