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