Sort multidimensional Array


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 2 Weeks Ago
Sort multidimensional Array

Hello

I have a problem.
I create a Multidimensional Array Like this:
Code:
        ENTRY["HOST1"]="$kunnum-$host"
        ENTRY["HOST2"]="$host"
    ENTRY["COMMENT"]="# $3"
    for key in "${!ENTRY[@]}"; do
        ENTRIES[$2"5",$key]=${ENTRY[$key]} # INDEX=IP(5)
    done

Code:
declare -p
declare -A ENTRIES=([205,HOST1]="unas15533" [205,HOST2]="unas" [1025,COMMENT]="# RDP-Terminal 2" [775,COMMENT]="# WLAN Access Point" [1015,IP]="111.122.133.101" [1025,IP]="111.122.133.102" [775,IP]="111.122.133.77" [205,COMMENT]="# UNAS" [775,HOST1]="15533-wlan" [1015,HOST2]="rdp1" [1015,HOST1]="15533-rdp1" [775,HOST2]="wlan" [1015,COMMENT]="# RDP-Terminal 1" [1025,HOST2]="rdp2" [1025,HOST1]="15533-rdp2" [205,IP]="111.122.133.20" )

Now i have to sort this by the "INDEX"
Code:
([205,HOST1]="unas15533"

in this case it would be 205
I have to sort all elements and then i want to give them formated out.

How can I sort this array?

Thank you so much for your help.




Moderator's Comments:
Mod Comment Please use CODE tags consistently as required by forum rules!

Last edited by RudiC; 2 Weeks Ago at 02:45 AM.. Reason: Added CODE tags.
# 2  
Old 2 Weeks Ago
Welcome to the forum.

Please become accustomed to provide decent context info of your problem.

It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, directory structures, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two including your own attempts at a solution, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.
# 3  
Old 2 Weeks Ago
Quote:
Originally Posted by Marti95
I have to sort all elements and then i want to give them formated out.

How can I sort this array?
In addition to what RudiC already said (and, by the way, if your programming language is not some shell - i assume that it is, but my assumption might be wrong - it would be nice to know which one that is). Notice, though, that most shells don't support multidimenisonal arrays and especially not assiociative arrays:

What exactly do you mean by "sort all the elements"? For a sorting you need some key - alphabetical, numerical, by date or whatever - and something to sort. An array, regardless of being multidimensional or not, contains several elements. The elements have an index - basically their position in the array - and some content. Do you want to sort the contents or the indices to be sorted?

If it is the indices: these are multi-value items in a multidimensional array and it is not clear which part of these take priority: i.e. if you have 3 entries, [1,10], [2,5] and [10,1], which one is supposed to be first? If the first element takes priority the order would be [1,10], [2,5], [10,1], if the second element takes priority you have [10,1], [2,5], [1,10], if the sum of both entries is the sort criteria it is [2,5], [1,10], [10,1], etc..

If it is the contents you are trying to sort you simply need to traverse through the indices of one dimension after the other using simple for-loops, i.e. for a 3-dimensional array with numerical indices (pseudocode):

Code:
for i in 1..10
     for j in 1..10
          for k in 1..10
               print array[i,j,k]
          next
     next
next

and sort the resulting list.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
nezabudka (2 Weeks Ago)
# 4  
Old 2 Weeks Ago
Code:
shuf -e ${!ENTRIES[@]} | sort -V | while read d; do echo $d=${ENTRIES[$d]}; done

--- Post updated at 12:01 ---

can play with the options of the "sort" command Smilie
This User Gave Thanks to nezabudka For This Post:
Marti95 (2 Weeks Ago)
# 5  
Old 2 Weeks Ago
Try also
Code:
echo "${!ENTRIES[*]}" | sort -n | while read IX; do printf "%-15s%-15s\n" $IX ${ENTRIES[$IX]}; done
205,COMMENT    # UNAS         
205,HOST1      unas15533      
205,HOST2      unas           
205,IP         111.122.133.20 
775,COMMENT    # WLAN Access Point
775,HOST1      15533-wlan     
775,HOST2      wlan           
775,IP         111.122.133.77 
1015,COMMENT   # RDP-Terminal 1
1015,HOST1     15533-rdp1     
1015,HOST2     rdp1           
1015,IP        111.122.133.101
1025,COMMENT   # RDP-Terminal 2
1025,HOST1     15533-rdp2     
1025,HOST2     rdp2           
1025,IP        111.122.133.102

This User Gave Thanks to RudiC For This Post:
nezabudka (2 Weeks Ago)
# 6  
Old 2 Weeks Ago
Thank you for your answers

And sorry for the few informations.
I used bash
Code:
#!/bin/bash

I have to sort this nummeric.
After sorting I want to give this out in a File
In this order
Code:
# IP - HOST1  - HOST2 - COMMENT

my problem is to find an efficent way for that.
Because i dont know the index of the array entry
It could be something between 10 and 2550

Code:
ENTRIES[INDEX,$key]

I gona try your methods for sorting the array. Thanks
# 7  
Old 2 Weeks Ago
Does reversing a sort key help?
Code:
$ IFS=$'\n'
$ echo "${!ENTRIES[*]}" | sort -t, -k1,1n -k2r | while read IX; do printf "%-15s%-15s\n" $IX ${ENTRIES[$IX]}; done
205,IP         111.122.133.20 
205,HOST2      unas           
205,HOST1      unas15533      
205,COMMENT    # UNAS         
775,IP         111.122.133.77 
775,HOST2      wlan           
775,HOST1      15533-wlan     
775,COMMENT    # WLAN Access Point
1015,IP        111.122.133.101
1015,HOST2     rdp1           
1015,HOST1     15533-rdp1     
1015,COMMENT   # RDP-Terminal 1
1025,IP        111.122.133.102
1025,HOST2     rdp2           
1025,HOST1     15533-rdp2     
 1025,COMMENT   # RDP-Terminal 2

Don't forget to reset your IFS afterwards.


EDIT: or, even better to get HOST1 and 2 in the right order:

Code:
echo "${!ENTRIES[*]}" | sort -t, -k1,1n -k2,2.2r -k2.3 | while read IX; do printf "%-15s%-15s\n" $IX ${ENTRIES[$IX]}; done

Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Multidimensional array annacreek Shell Programming and Scripting 4 08-19-2018 12:44 AM
Multidimensional array:awk error genome Shell Programming and Scripting 7 12-07-2017 11:23 AM
How to deal with multidimensional array in awk? Akshay Hegde Shell Programming and Scripting 5 03-28-2013 04:19 AM
SQL database call into Multidimensional Array using Perl Script eazyeddie22 UNIX and Linux Applications 3 01-04-2012 12:48 AM
gawk - How to loop through multidimensional array? trey85stang Shell Programming and Scripting 5 10-03-2011 02:02 PM
Multidimensional arrays and sort. sepoto Programming 2 01-30-2011 09:41 AM
Multidimensional array of strings with vector. sepoto Programming 5 12-29-2010 05:22 PM
multidimensional array in awk ahmedwaseem2000 Shell Programming and Scripting 2 09-20-2010 08:23 PM
perl-data from file save to multidimensional array pp-zz Shell Programming and Scripting 1 07-29-2010 11:11 AM
Perl help: Creating a multidimensional array of subdirectories and its contents shwang3 Shell Programming and Scripting 6 04-06-2010 03:03 AM
multidimensional array using c++ vector carl.alv Programming 0 04-16-2009 04:27 AM
C programming working with multidimensional array littleboyblu Programming 6 03-14-2009 05:44 AM
AWK multidimensional array sinpeak Shell Programming and Scripting 4 12-17-2008 02:58 AM
Awk multidimensional Array user_prady Shell Programming and Scripting 1 09-01-2008 11:50 PM
multidimensional array in perl prkfriryce Shell Programming and Scripting 9 12-01-2007 03:33 PM