Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting an array 1

Status
Not open for further replies.

nychris

MIS
Dec 4, 2004
103
US
I'm having trouble getting an array to sort correctly. It looks like this...

Code:
@unsorted = (
'1.0_A0 - name -  Aug 01 2006 08:50',
'1.4_E3 - name.10 -  Mar 07 2007 16:55',
'1.5_F1 - name.11 -  Mar 23 2007 18:17',
'1.5_F8 - name.15 -  Sep 12 2007 17:16',
'1.5_F10 - name.16 -  Sep 12 2007 17:27',
'1.5_F12 - name.17 -  Sep 26 2007 17:15',
'1.1_B0 - name.2 -  Aug 07 2006 10:00',
'1.5_F30 - name.28 -  Mar 10 2008 17:56',
'1.5_F7 - name.14 -  Sep 11 2007 17:16',
'1.5_F31 - name.29 -  Apr 25 2008 17:43',
'1.2_C2 - name.3 -  Oct 02 2006 09:17',
'1.3_D1 - name.5 -  Dec 01 2006 17:31'
);

If I were to sort and print this like this....
Code:
@sorted = sort @unsorted;
print "$_\n" for @sorted;

It doesn't work out...
Code:
1.0_A0 - name -  Aug 01 2006 08:50
1.1_B0 - name.2 -  Aug 07 2006 10:00
1.2_C2 - name.3 -  Oct 02 2006 09:17
1.3_D1 - name.5 -  Dec 01 2006 17:31
1.4_E3 - name.10 -  Mar 07 2007 16:55
1.5_F1 - name.11 -  Mar 23 2007 18:17
1.5_F10 - name.16 -  Sep 12 2007 17:27
1.5_F12 - name.17 -  Sep 26 2007 17:15
1.5_F30 - name.28 -  Mar 10 2008 17:56
1.5_F31 - name.29 -  Apr 25 2008 17:43
1.5_F7 - name.14 -  Sep 11 2007 17:16
1.5_F8 - name.15 -  Sep 12 2007 17:16

1.5_F31 is higher than 1.5_F8, for example. How can I sort this the right way? I guess I'll need to add a zero to any version numbers that only have 1 digit after the letter so 1.5_F8 would temporarily become 1.5_F08. Then it would get sorted, and before its printed any added 0's will need to be removed. Any ideas on how to go about this?

Thanks!




--
Chris
RHCE, SCSA, AIX5L, LPIC, CNE, CCNA, MCSE
 
not well tested:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]warnings[/green][red];[/red]
[black][b]use[/b][/black] [green]strict[/green][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@unsorted[/blue] = [red]([/red]
[red]'[/red][purple]1.0_A0 - name -  Aug 01 2006 08:50[/purple][red]'[/red],
[red]'[/red][purple]1.4_E3 - name.10 -  Mar 07 2007 16:55[/purple][red]'[/red],
[red]'[/red][purple]1.5_F1 - name.11 -  Mar 23 2007 18:17[/purple][red]'[/red],
[red]'[/red][purple]1.5_F8 - name.15 -  Sep 12 2007 17:16[/purple][red]'[/red],
[red]'[/red][purple]1.5_F10 - name.16 -  Sep 12 2007 17:27[/purple][red]'[/red],
[red]'[/red][purple]1.5_F12 - name.17 -  Sep 26 2007 17:15[/purple][red]'[/red],
[red]'[/red][purple]1.1_B0 - name.2 -  Aug 07 2006 10:00[/purple][red]'[/red],
[red]'[/red][purple]1.5_F30 - name.28 -  Mar 10 2008 17:56[/purple][red]'[/red],
[red]'[/red][purple]1.5_F7 - name.14 -  Sep 11 2007 17:16[/purple][red]'[/red],
[red]'[/red][purple]1.5_F31 - name.29 -  Apr 25 2008 17:43[/purple][red]'[/red],
[red]'[/red][purple]1.2_C2 - name.3 -  Oct 02 2006 09:17[/purple][red]'[/red],
[red]'[/red][purple]1.3_D1 - name.5 -  Dec 01 2006 17:31[/purple][red]'[/red]
[red])[/red][red];[/red]

[black][b]my[/b][/black] [blue]@sorted[/blue] = [url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red][blue]$_[/blue]->[red][[/red][fuchsia]3[/fuchsia][red]][/red][red]}[/red]
             [url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [red]{[/red][blue]$a[/blue]->[red][[/red][fuchsia]0[/fuchsia][red]][/red] <=> [blue]$b[/blue]->[red][[/red][fuchsia]0[/fuchsia][red]][/red] || [blue]$a[/blue]->[red][[/red][fuchsia]1[/fuchsia][red]][/red] cmp [blue]$b[/blue]->[red][[/red][fuchsia]1[/fuchsia][red]][/red] || [blue]$a[/blue]->[red][[/red][fuchsia]2[/fuchsia][red]][/red] <=> [blue]$b[/blue]->[red][[/red][fuchsia]2[/fuchsia][red]][/red][red]}[/red]
             [black][b]map[/b][/black] [red]{[/red][red]/[/red][purple]^([^_]+)_([A-Z]+)([purple][b]\d[/b][/purple]+) [/purple][red]/[/red][red];[/red] [red][[/red][blue]$1[/blue],[blue]$2[/blue],[blue]$3[/blue],[blue]$_[/blue][red]][/red]  [red]}[/red] [blue]@unsorted[/blue][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$_[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red] [olive][b]for[/b][/olive] [blue]@sorted[/blue][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
the ouput was:

Code:
1.0_A0 - name -  Aug 01 2006 08:50
1.1_B0 - name.2 -  Aug 07 2006 10:00
1.2_C2 - name.3 -  Oct 02 2006 09:17
1.3_D1 - name.5 -  Dec 01 2006 17:31
1.4_E3 - name.10 -  Mar 07 2007 16:55
1.5_F1 - name.11 -  Mar 23 2007 18:17
1.5_F7 - name.14 -  Sep 11 2007 17:16
1.5_F8 - name.15 -  Sep 12 2007 17:16
1.5_F10 - name.16 -  Sep 12 2007 17:27
1.5_F12 - name.17 -  Sep 26 2007 17:15
1.5_F30 - name.28 -  Mar 10 2008 17:56
1.5_F31 - name.29 -  Apr 25 2008 17:43


first it sorts the x.x part then the alpha letter then the digits after the alpha letter. If this a real long array you may want to try and find a more efficient way of making a list of keys to sort.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
KevinADC, that worked great! Thanks for your help! I'll have to look over that code as its a little over my head to fully understand it, haha.

--
Chris
RHCE, SCSA, AIX5L, LPIC, CNE, CCNA, MCSE
 
You're welcome

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top