Sponsored Content
Full Discussion: sorting numeric array
Top Forums Shell Programming and Scripting sorting numeric array Post 302442820 by phoeberunner on Thursday 5th of August 2010 01:52:56 PM
Old 08-05-2010
sorting numeric array

Hi,

I would like to do the following sorting, but the output is not what i expected. Why 222 and 2222 are not at the last two elements of array?

Code:
awk 'BEGIN{a[1]="22";a[2]="2222";a[3]="33";a[4]="44";a[5]="222";a[6]="11";a[7]="22";a[8]="33";asort(a); for (i=1;i<=8;i++) print a[i]}'
11
22
22
222
2222
33
33
44

thanks,
phoebe
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl: Sorting an associative array

Hi, When using sort on an associative array: foreach $key (sort(keys(%opalfabet))){ $value = $opalfabet{$key}; $result .= $value; } How does it handle double values? It seems to me that it removes them, is that true? If so, is there a way to get... (2 Replies)
Discussion started by: tine
2 Replies

2. Shell Programming and Scripting

sorting data using array in ksh

plz help me..........i have a ksh script that sorts data in ascending order. the 1st half is correct,but for the line no 31 its showing problem 1 #!/bin/ksh 2 3 4 5 echo "Enter the array length" 6 read num 7 8 9 echo "enter the... (4 Replies)
Discussion started by: ali560045
4 Replies

3. Shell Programming and Scripting

Sorting with multiple numeric keys

Data I want to sort :- 1 10 jj Y 2 100 vv B 19 5 jj A 1 11 hq D 3 8 op X 44 78 ds GG 1 8 hq D and want to sort based on the first 2 columns - which hold numeric values. Am using : cat filename | sort -nk 1,2 But the result is :- 1 10 jj Y 1 11 hq D (1 Reply)
Discussion started by: sinpeak
1 Replies

4. Shell Programming and Scripting

Sorting value frequency within an array

How is it possible to sort different nummeric values within an Array. But i don`t want the highest or the lowest. I need the most frequently occurring value. For examble: My Array has to following values = (200 404 404 500 404 404 404 200 404) The result should be 404 The values are... (3 Replies)
Discussion started by: 2retti
3 Replies

5. Shell Programming and Scripting

perl array sorting

Hi All, I have an array in perl as @match = (201001,201002,201001,201002); I am trying to sort this array as @match = sort(@match); print "@match"; I dont see the output sorted any answers I also tried another way, but still the results are not sorted foreach my $match (sort { $a... (2 Replies)
Discussion started by: bsdeepu
2 Replies

6. Shell Programming and Scripting

sorting multi dimensional array

Hi there, Can someone let me know how to sort the 2 dimensional array below by column 1 then by column 2? 22 55 2222 2230 33 66 44 58 222 240 11 25 22 60 33 45 output: 11 25 22 55 22 60 33 45 33 66 44 58 (6 Replies)
Discussion started by: phoeberunner
6 Replies

7. Shell Programming and Scripting

Sorting the alpha numeric results of Hash

Hi All I've got a perl script that I'm having a problem with when it prints the output of a hash. Some background. I'm trying to merge two file with a similar structure but with different data. Here is a portion of the script I'm using. while (<INPUT>) { my... (0 Replies)
Discussion started by: kingpin2502
0 Replies

8. Shell Programming and Scripting

Finding BEGINNING & ENDING positions of sequentially increasing sublists from a perl numeric array

I have got an Perl array like: @array = (1,2,3,4,5,6,1,2,3,4,1,2,1,2,3,4,5,6,7,8,9...............) This numeric sequence will be always sequentially increasing, unless it encounters, The beginning of the new sequentially increasing numeric sequence. SO in this array we get sequentially... (5 Replies)
Discussion started by: teknokid1
5 Replies

9. Shell Programming and Scripting

sorting left-justified numeric values

I have a file which looks roughly like this: 996 mmmmmmm 996 xxxxxxxxxxxxx 99600 ssssssssss 9964 fffffffffffff and would like to sort it numerically on the first field. I tried: sort -nr --key=1 .... The output I get is: 99600 ssssssssss 9964 ... (3 Replies)
Discussion started by: rovf
3 Replies

10. Shell Programming and Scripting

Sorting file based on a numeric column

Hi, I use UBUNTU 12.04. I have a file with this structure: Name 2 1245787 A G 12 14 12 14 .... Name 1 1245789 C T 13 12 12 12..... I would like to sort my file based on the second column so to have this output for example: Name 1 1245789 C T 13 12 12 12..... Name 2 1245787 A G 12 14... (4 Replies)
Discussion started by: Homa
4 Replies
Sort::Key(3pm)						User Contributed Perl Documentation					    Sort::Key(3pm)

NAME
Sort::Key - the fastest way to sort anything in Perl SYNOPSIS
use Sort::Key qw(keysort nkeysort ikeysort); @by_name = keysort { "$_->{surname} $_->{name}" } @people; # sorting by a numeric key: @by_age = nkeysort { $_->{age} } @people; # sorting by a numeric integer key: @by_sons = ikeysort { $_->{sons} } @people; DESCRIPTION
Sort::Key provides a set of functions to sort lists of values by some calculated key value. It is faster (usually much faster) and uses less memory than other alternatives implemented around perl sort function (ST, GRT, etc.). Multikey sorting functionality is also provided via the companion modules Sort::Key::Multi, Sort::Key::Maker and Sort::Key::Register. FUNCTIONS This module provides a large number of sorting subroutines but they are all variations off the "keysort" one: @sorted = keysort { CALC_KEY($_) } @data that is conceptually equivalent to @sorted = sort { CALC_KEY($a) cmp CALC_KEY($b) } @data and where "CALC_KEY($_)" can be any expression to extract the key value from $_ (not only a subroutine call). For instance, some variations are "nkeysort" that performs a numeric comparison, "rkeysort" that orders the data in descending order, "ikeysort" and "ukeysort" that are optimized versions of "nkeysort" that can be used when the keys are integers or unsigned integers respectively, etc. Also, inplace versions of the sorters are provided. For instance keysort_inplace { CALC_KEY($_) } @data that is equivalent to @data = keysort { CALC_KEY($_) } @data but being (a bit) faster and using less memory. The full list of subroutines that can be imported from this module follows: keysort { CALC_KEY } @array returns the elements on @array sorted by the key calculated applying "{ CALC_KEY }" to them. Inside "{ CALC_KEY }", the object is available as $_. For example: @a=({name=>john, surname=>smith}, {name=>paul, surname=>belvedere}); @by_name=keysort {$_->{name}} @a; This function honours the "use locale" pragma. nkeysort { CALC_KEY } @array similar to keysort but compares the keys numerically instead of as strings. This function honours the "use integer" pragma, i.e.: use integer; my @s=(2.4, 2.0, 1.6, 1.2, 0.8); my @ns = nkeysort { $_ } @s; print "@ns " prints 0.8 1.6 1.2 2.4 2 rnkeysort { CALC_KEY } @array works as nkeysort, comparing keys in reverse (or descending) numerical order. ikeysort { CALC_KEY } @array works as keysort but compares the keys as integers (32 bits or more, no checking is performed for overflows). rikeysort { CALC_KEY } @array works as ikeysort, but in reverse (or descending) order. ukeysort { CALC_KEY } @array works as keysort but compares the keys as unsigned integers (32 bits or more). For instance, it can be used to efficiently sort IP4 addresses: my @data = qw(1.2.3.4 4.3.2.1 11.1.111.1 222.12.1.34 0.0.0.0 255.255.255.0) 127.0.0.1); my @sorted = ukeysort { my @a = split /./; (((($a[0] << 8) + $a[1] << 8) + $a[2] << 8) + $a[3]) } @data; rukeysort { CALC_KEY } @array works as ukeysort, but in reverse (or descending) order. keysort_inplace { CALC_KEY } @array nkeysort_inplace { CALC_KEY } @array ikeysort_inplace { CALC_KEY } @array ukeysort_inplace { CALC_KEY } @array rkeysort_inplace { CALC_KEY } @array rnkeysort_inplace { CALC_KEY } @array rikeysort_inplace { CALC_KEY } @array rukeysort_inplace { CALC_KEY } @array work as the corresponding keysort functions but sorting the array inplace. rsort @array nsort @array rnsort @array isort @array risort @array usort @array rusort @array rsort_inplace @array nsort_inplace @array rnsort_inplace @array isort_inplace @array risort_inplace @array usort_inplace @array rusort_inplace @array are simplified versions of its keysort cousins. They use the own values as the sorting keys. For instance those constructions are equivalent: @sorted = nsort @foo; @sorted = nkeysort { $_ } @foo; @sorted = sort { $a <=> $b } @foo; multikeysorter(@types) multikeysorter_inplace(@types) multikeysorter(&genkeys, @types) multikeysorter_inplace(&genkeys, @types) are the low level interface to the multikey sorting functionality (normally, you should use Sort::Key::Maker and Sort::Key::Register or Sort::Key::Multi instead). They get a list of keys descriptions and return a reference to a multikey sorting subroutine. Types accepted by default are: string, str, locale, loc, integer, int, unsigned_integer, uint, number, num and support for additional types can be added via the register_type subroutine available from Sort::Key::Types or the more friendly interface available from Sort::Key::Register. Types can be preceded by a minus sign to indicate descending order. If the first argument is a reference to a subroutine it is used as the multikey extraction function. If not, the generated sorters expect one as their first argument. Example: my $sorter1 = multikeysorter(sub {length $_, $_}, qw(int str)); my @sorted1 = &$sorter1(qw(foo fo o of oof)); my $sorter2 = multikeysorter(qw(int str)); my @sorted2 = &$sorter2(sub {length $_, $_}, qw(foo fo o of oof)); SEE ALSO
perl sort function, integer, locale. Companion modules Sort::Key::Multi, Sort::Key::Register, Sort::Key::Maker and Sort::Key::Natural. Sort::Key::IPv4, Sort::Key::DateTime and Sort::Key::OID modules add support for additional datatypes to Sort::Key. Sort::Key::External allows one to sort huge lists that do not fit in the available memory. Other interesting Perl sorting modules are Sort::Maker, Sort::Naturally and Sort::External. SUPPORT
To report bugs, send me and email or use the CPAN bug tracking system at <http://rt.cpan.org>. Commercial support Commercial support, professional services and custom software development around this module are available through my current company. Drop me an email with a rough description of your requirements and we will get back to you ASAP. My wishlist If you like this module and you're feeling generous, take a look at my Amazon Wish List: <http://amzn.com/w/1WU1P6IR5QZ42> COPYRIGHT AND LICENSE
Copyright (C) 2005-2007, 2012 by Salvador Fandin~o, <sfandino@yahoo.com>. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available. perl v5.14.2 2012-06-30 Sort::Key(3pm)
All times are GMT -4. The time now is 08:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy