troubleshooting Question

Sorting objects within objects?

Avatar of sargento
sargento asked on
JavaScript
2 Comments1 Solution269 ViewsLast Modified:
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.


ASKER CERTIFIED SOLUTION
dmethvin

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros