How To Sort Array of Hashes ??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How To Sort Array of Hashes ??
# 1  
Old 05-06-2005
How To Sort Array of Hashes ??

Some one plz help me how to sort an array of hashes .....

for e.g i have an array as

@AoH = (
{
ques => 10,
marks => 32,
},
{
ques => 32,
marks => 22,
},
{
ques => 2,
marks => 41,
},
);

now i want to sort this array with increasing value of "ques" ..... plz help

Last edited by coolguyshail; 05-06-2005 at 06:46 AM.. Reason: question not clear
# 2  
Old 05-06-2005
If this is Perl, this is an array of hash references. You can't strictly make an array of hashes in Perl, as this will eventually become a single array.

It's actually very simple:

Code:
@sorted = sort { $$a{'ques'} <=> $$b{'ques'} } @AoH;

EDIT: Actually the perldsc manpage has all the details (hopefully it won't overwhelm you!)

Last edited by cbkihong; 05-06-2005 at 09:50 AM.. Reason: Additional Information
# 3  
Old 05-09-2005
one more problem

one more thing ... wat if i want to put 'ques' 32 and 'ques' 18 together and other 'ques' sorted ...
# 4  
Old 05-09-2005
Perl's sort function is immensely powerful (and somewhat difficult to explain) that lets you sort in any way you desire. Theoretically you can put the condition (the comparator) in the sort {} block to generate a custom ordering for your case. But your question is not precise. And you cannot simply say keep 32 and 18 together, because what should be the ordering of other array items with respect to 32 and 18, respectively? And the order of 32 and 18? If these questions are not defined, the condition in the sort {} block will not be accurate to establish a total ordering, and the sorted results will then be non-deterministic (i.e. simply wrong).

Mathematically speaking, given a total of n items to sort, you need to define all possible permutations of any two items in the block, i.e. n * n, and the ordering should be absolutely consistent. So, if you define a certain item x(i) -> 32 and 32 -> 18, the condition in the block should never, ever, generate the order 18 -> x(i), or any other items preceding x(i).

In general, if a given sort order is not intuitive, and you cannot specify an algorithm to establish the total ordering, I will suggest getting the normally sorted results and manipulate the rest yourself with another array. For this, I guess you should try to do this yourself.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort multidimensional Array

Hello I have a problem. I create a Multidimensional Array Like this: ENTRY="$kunnum-$host" ENTRY="$host" ENTRY="# $3" for key in "${!ENTRY}"; do ENTRIES=${ENTRY} # INDEX=IP(5) donedeclare -p declare -A ENTRIES=(="unas15533" ="unas" ="# RDP-Terminal 2"... (12 Replies)
Discussion started by: Marti95
12 Replies

2. UNIX for Beginners Questions & Answers

Array length: ls and sort

Hi there, I'm listing files and sorting them. When I try to get length of array variable in which these files are stored I get 1 as value. That's weird. files_info="$(find $input_dir -name "*_CHR$i.info" | sort )" printf ${#files_info}"\n" #print length #--loop through... (6 Replies)
Discussion started by: genome
6 Replies

3. Shell Programming and Scripting

Perl hash of hashes anonymous array

Hello experts. I'm having problems with a snippet of code. I was hoping to get help/advice to correct. A file that this script parses has changed to the point where I can no longer use a scalar, it looks as though I need to create an array for a hash of hashes below. The first output of... (1 Reply)
Discussion started by: timj123
1 Replies

4. Shell Programming and Scripting

Perl : array of hashes help

Hi, I have array of hashes and each key has array like below. @array1 = ( { 'url' => , 'bill' => }, { 'url' => , 'bill' => }, { 'url' => , ... (0 Replies)
Discussion started by: ragilla
0 Replies

5. Shell Programming and Scripting

awk array sort

I have a file as below Input File: 5/11/2012, dir1, 134 5/11/2012, dir2, 2341 5/11/2012, dir3, 2134 5/11/2012, dir4, 2334 5/12/2012, dir1, 234 5/12/2012, dir2, 2341 5/12/2012, dir3, 2334 5/13/2012, dir1, 234 5/13/2012, dir2, 2341 5/13/2012, dir3, 2334 5/13/2012, dir4, 21134 Now... (6 Replies)
Discussion started by: chakrapani
6 Replies

6. Shell Programming and Scripting

Creating Hashes of Hashes of Array

Hi folks, I have a structure as mentioned below in a configuration file. <Component> Comp1: { item1:data,someUniqueAttribute; item2:data,someUniqueAttribute, } Comp2: { item3:data,someUniqueAttribute; ... (1 Reply)
Discussion started by: ckv84
1 Replies

7. Shell Programming and Scripting

sort piping to awk array - help please

Hi I'm just learning programming and need some help. I've taken a data file which has a list of numbers eg: 3 5 32 533 13 2 And I've used sort -n and to sort and then piped it to awk to arrange into an array. #!/bin/sh sort -n data.txt | awk ' { array=$1 } (4 Replies)
Discussion started by: EL_Chemso
4 Replies

8. Shell Programming and Scripting

perl sort array by field

Hi Everyone, Any simple code can simplify the code below, please advice. Thanks # cat 2.pl #!/usr/bin/perl use warnings; use strict; my @aaaaa = <DATA>; my @uids; foreach (@aaaaa) { my @ccccc = split (",", $_); push @uids, $ccccc;... (3 Replies)
Discussion started by: jimmy_y
3 Replies

9. UNIX for Dummies Questions & Answers

Problem with sort-of array in SH

Hello to everyone! I'm really new in shell scripting and I'm experiencing a very odd problem. This is my first post in this forum, hope you can help! I know that declaring arrays in Bourne Shell is impossible. But this is where I start having problems - system administrator did not install... (8 Replies)
Discussion started by: onstock
8 Replies

10. Shell Programming and Scripting

Array split function & hashes

Hi, If this is the array that is being returned to me: How would I get the values for each of the 3 records? This works for 1 Record: foreach $item (@results) { ($id, $id2, $name, $date, $email) = split(/\|/, $item, 5); print "$name<br>"; } (2 Replies)
Discussion started by: novera
2 Replies
Login or Register to Ask a Question