xref: /AOO41X/main/solenv/bin/modules/installer/sorter.pm (revision 9780544fa6b4c85f7d9b48452f58c7da854fc9a5)
1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22
23
24package installer::sorter;
25
26#########################################
27# Sorting an array of hashes
28#########################################
29
30sub sorting_array_of_hashes
31{
32    my ($arrayref, $sortkey) = @_;
33
34    for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
35    {
36        my $onehashunder = ${$arrayref}[$i];
37        my $sortvalueunder = $onehashunder->{$sortkey};
38
39        for ( my $j = $i + 1; $j <= $#{$arrayref}; $j++ )
40        {
41            my $onehashover = ${$arrayref}[$j];
42            my $sortvalueover = $onehashover->{$sortkey};
43
44            if ( $sortvalueunder gt $sortvalueover)
45            {
46                ${$arrayref}[$i] = $onehashover;
47                ${$arrayref}[$j] = $onehashunder;
48
49                $onehashunder = $onehashover;
50                $sortvalueunder = $sortvalueover;
51            }
52        }
53    }
54}
55
56######################################################
57# Sorting an array of hashes with a numerical value
58######################################################
59
60sub sort_array_of_hashes_numerically
61{
62    my ($arrayref, $sortkey) = @_;
63
64    for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
65    {
66        my $onehashunder = ${$arrayref}[$i];
67        my $sortvalueunder = $onehashunder->{$sortkey};
68
69        for ( my $j = $i + 1; $j <= $#{$arrayref}; $j++ )
70        {
71            my $onehashover = ${$arrayref}[$j];
72            my $sortvalueover = $onehashover->{$sortkey};
73
74            if ( $sortvalueunder > $sortvalueover)
75            {
76                ${$arrayref}[$i] = $onehashover;
77                ${$arrayref}[$j] = $onehashunder;
78
79                $onehashunder = $onehashover;
80                $sortvalueunder = $sortvalueover;
81            }
82        }
83    }
84}
85
86#########################################
87# Sorting an array of of strings
88#########################################
89
90sub sorting_array_of_strings
91{
92    my ($arrayref) = @_;
93
94    for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
95    {
96        my $onestringunder = ${$arrayref}[$i];
97
98        for ( my $j = $i + 1; $j <= $#{$arrayref}; $j++ )
99        {
100            my $onestringover = ${$arrayref}[$j];
101
102            if ( $onestringunder gt $onestringover)
103            {
104                ${$arrayref}[$i] = $onestringover;
105                ${$arrayref}[$j] = $onestringunder;
106                $onestringunder = $onestringover;
107            }
108        }
109    }
110}
111
1121;
113