Skip to content

Instantly share code, notes, and snippets.

@soh-i
Last active October 8, 2015 18:38
Show Gist options
  • Select an option

  • Save soh-i/3372382 to your computer and use it in GitHub Desktop.

Select an option

Save soh-i/3372382 to your computer and use it in GitHub Desktop.
文字と数字の混在したデータをソートする
#!/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