1#*************************************************************************
2#
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# Copyright 2000, 2010 Oracle and/or its affiliates.
6#
7# OpenOffice.org - a multi-platform office productivity suite
8#
9# This file is part of OpenOffice.org.
10#
11# OpenOffice.org is free software: you can redistribute it and/or modify
12# it under the terms of the GNU Lesser General Public License version 3
13# only, as published by the Free Software Foundation.
14#
15# OpenOffice.org is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU Lesser General Public License version 3 for more details
19# (a copy is included in the LICENSE file that accompanied this code).
20#
21# You should have received a copy of the GNU Lesser General Public License
22# version 3 along with OpenOffice.org.  If not, see
23# <http://www.openoffice.org/license.html>
24# for a copy of the LGPLv3 License.
25#
26#*************************************************************************
27
28package installer::sorter;
29
30#########################################
31# Sorting an array of hashes
32#########################################
33
34sub sorting_array_of_hashes
35{
36	my ($arrayref, $sortkey) = @_;
37
38	for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
39	{
40		my $onehashunder = ${$arrayref}[$i];
41		my $sortvalueunder = $onehashunder->{$sortkey};
42
43		for ( my $j = $i + 1; $j <= $#{$arrayref}; $j++ )
44		{
45			my $onehashover = ${$arrayref}[$j];
46			my $sortvalueover = $onehashover->{$sortkey};
47
48			if ( $sortvalueunder gt $sortvalueover)
49			{
50				${$arrayref}[$i] = $onehashover;
51				${$arrayref}[$j] = $onehashunder;
52
53				$onehashunder = $onehashover;
54				$sortvalueunder = $sortvalueover;
55			}
56		}
57	}
58}
59
60######################################################
61# Sorting an array of hashes with a numerical value
62######################################################
63
64sub sort_array_of_hashes_numerically
65{
66	my ($arrayref, $sortkey) = @_;
67
68	for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
69	{
70		my $onehashunder = ${$arrayref}[$i];
71		my $sortvalueunder = $onehashunder->{$sortkey};
72
73		for ( my $j = $i + 1; $j <= $#{$arrayref}; $j++ )
74		{
75			my $onehashover = ${$arrayref}[$j];
76			my $sortvalueover = $onehashover->{$sortkey};
77
78			if ( $sortvalueunder > $sortvalueover)
79			{
80				${$arrayref}[$i] = $onehashover;
81				${$arrayref}[$j] = $onehashunder;
82
83				$onehashunder = $onehashover;
84				$sortvalueunder = $sortvalueover;
85			}
86		}
87	}
88}
89
90#########################################
91# Sorting an array of of strings
92#########################################
93
94sub sorting_array_of_strings
95{
96	my ($arrayref) = @_;
97
98	for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
99	{
100		my $onestringunder = ${$arrayref}[$i];
101
102		for ( my $j = $i + 1; $j <= $#{$arrayref}; $j++ )
103		{
104			my $onestringover = ${$arrayref}[$j];
105
106			if ( $onestringunder gt $onestringover)
107			{
108				${$arrayref}[$i] = $onestringover;
109				${$arrayref}[$j] = $onestringunder;
110				$onestringunder = $onestringover;
111			}
112		}
113	}
114}
115
1161;
117