HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Why doesn't sort work?
Operating System - HP-UX
1833875
Members
2029
Online
110063
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2002 10:31 AM
04-23-2002 10:31 AM
Hello,
I'm trying my hand at perl and I have a problem. I have an array with the values
100.9, 10.3, 12, 50, -38
I do this:
@array2 = sort @array1;
Instead of coming out in correct order, array2 prints out like this:
-38, 10.3, 100.9, 12, 50
What is wrong with perl's sort?
Thanks, John W.
I'm trying my hand at perl and I have a problem. I have an array with the values
100.9, 10.3, 12, 50, -38
I do this:
@array2 = sort @array1;
Instead of coming out in correct order, array2 prints out like this:
-38, 10.3, 100.9, 12, 50
What is wrong with perl's sort?
Thanks, John W.
At least I have a job.
Solved! Go to Solution.
1 REPLY 1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2002 10:36 AM
04-23-2002 10:36 AM
Solution
Hi John:
The simple answer is that sort is doing just what you are telling it to do. In the absense of a comparison routine, sort sorts in lexical order but you obviously want numeric order. The cure is quite simple: supply a comparison subroutine. In this case, we can use the inline method via the 'spaceship' operator.
@array2 = sort {$a <=> $b} @array1;
In the case, < 0 is returned if a < b; 0 if a = b; and > 0 is a > b. You can reverse the order of the sort by using {$b <=> $a}.
This should fix you, Clay
The simple answer is that sort is doing just what you are telling it to do. In the absense of a comparison routine, sort sorts in lexical order but you obviously want numeric order. The cure is quite simple: supply a comparison subroutine. In this case, we can use the inline method via the 'spaceship' operator.
@array2 = sort {$a <=> $b} @array1;
In the case, < 0 is returned if a < b; 0 if a = b; and > 0 is a > b. You can reverse the order of the sort by using {$b <=> $a}.
This should fix you, Clay
If it ain't broke, I can fix that.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP