Last active
October 8, 2015 18:38
-
-
Save soh-i/3372382 to your computer and use it in GitHub Desktop.
文字と数字の混在したデータをソートする
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use strict; | |
| use warnings; | |
| my @list = ( 3, '_hoge', 1, 'Y', 'X', 2, 23, 64, 123, 90, 11, 12, 21, 23); | |
| my @sorted = sort chromosome_sort @list; | |
| print join(", ", @sorted)."\n"; | |
| sub chromosome_sort { | |
| if ( _is_number($a) && _is_number($b) ) { | |
| # is numetric | |
| return $a <=> $b; | |
| } | |
| else { | |
| # is string | |
| return $a cmp $b; | |
| } | |
| } | |
| sub _is_number { | |
| if ( ( $_[0] ^ $_[0] ) eq 0 ) { | |
| # is numetric | |
| return 1; | |
| } | |
| else { | |
| # is string | |
| return 0; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment