Advertisement

11.23.2005 at 05:06PM PST, ID: 21641773
[x]
Attachment Details

tricky sort question got me stumped (quick, ozo, help :)

Asked by kandura in Perl Programming Language

Tags: question, sort, tricky

=pod

I've got an array that I want to sort in a tree-like fashion. Each element consists of a hashref with a path and label node. These should be sorted by path, but elements in the same "directory" should be sorted alphabetically. Here's an example data structure:

=cut

@r = (
    { path => './001',         label => 'base'   },
    { path => './001/001',          label => 'last'   },
    { path => './001/002',     label => 'first'  },
    { path => './001/002/001', label => 'last'   },
    { path => './001/002/002', label => 'first'  },
    { path => './002',              label => 'second' },
    { path => './002/001',          label => 'last'   },
    { path => './002/002',          label => 'first'  },
    { path => './003',              label => 'third'  },
    { path => './004',              label => 'fourth' },
);

=pod

As you can see, this is sorted by path, But I'd like it sorted like this:

@s = (
    { path => './001',         label => 'base'   },
    { path => './001/002',     label => 'first'  },
    { path => './001/002/002', label => 'first'  },
    { path => './001/002/001', label => 'last'   },
    { path => './004',              label => 'fourth' },
    { path => './001/001',          label => 'last'   },
    { path => './002',              label => 'second' },
    { path => './002/002',          label => 'first'  },
    { path => './002/001',          label => 'last'   },
    { path => './003',              label => 'third'  },
);

So basically: sort items with the same path component (that is, everything except what's after the last /) alphabetically, otherwise sort by full path.
But I can't seem to get it right.Start Free Trial
 
Loading Advertisement...
 
[+][-]11.23.2005 at 05:07PM PST, ID: 15354369

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.23.2005 at 09:41PM PST, ID: 15355115

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.23.2005 at 09:52PM PST, ID: 15355142

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.23.2005 at 10:46PM PST, ID: 15355243

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.24.2005 at 06:57AM PST, ID: 15356851

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.24.2005 at 07:17AM PST, ID: 15356956

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.24.2005 at 09:23AM PST, ID: 15357498

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.24.2005 at 09:30AM PST, ID: 15357516

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.24.2005 at 01:46PM PST, ID: 15358220

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.24.2005 at 01:49PM PST, ID: 15358223

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.24.2005 at 01:59PM PST, ID: 15358240

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.24.2005 at 02:21PM PST, ID: 15358300

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.24.2005 at 04:07PM PST, ID: 15358512

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.25.2005 at 01:39AM PST, ID: 15359841

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Perl Programming Language
Tags: question, sort, tricky
Sign Up Now!
Solution Provided By: ozo
Participating Experts: 2
Solution Grade: A
 
 
[+][-]11.25.2005 at 04:47AM PST, ID: 15360389

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.26.2005 at 03:27AM PST, ID: 15364207

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.26.2005 at 05:35PM PST, ID: 15366878

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32