Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Q
question2answer
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
outils
question2answer
Commits
4f8dc92b
Commit
4f8dc92b
authored
Feb 01, 2016
by
Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Coding style (util/sort.php)
parent
baf44c4e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
115 additions
and
122 deletions
+115
-122
sort.php
qa-include/util/sort.php
+115
-122
No files found.
qa-include/util/sort.php
View file @
4f8dc92b
...
...
@@ -20,150 +20,144 @@
More about this license: http://www.question2answer.org/license.php
*/
if
(
!
defined
(
'QA_VERSION'
))
{
// don't allow this page to be requested directly from browser
header
(
'Location: ../'
);
exit
;
}
function
qa_sort_by
(
&
$array
,
$by1
,
$by2
=
null
)
/*
Sort the $array of inner arrays by sub-element $by1 of each inner array, and optionally then by sub-element $by2
*/
{
global
$qa_sort_by_1
,
$qa_sort_by_2
;
if
(
!
defined
(
'QA_VERSION'
))
{
// don't allow this page to be requested directly from browser
header
(
'Location: ../'
);
exit
;
}
/**
* Sort the $array of inner arrays by sub-element $by1 of each inner array, and optionally then by sub-element $by2
*/
function
qa_sort_by
(
&
$array
,
$by1
,
$by2
=
null
)
{
global
$qa_sort_by_1
,
$qa_sort_by_2
;
$qa_sort_by_1
=
$by1
;
$qa_sort_by_2
=
$by2
;
uasort
(
$array
,
'qa_sort_by_fn'
);
}
/**
* Function used in uasort to implement qa_sort_by()
*/
function
qa_sort_by_fn
(
$a
,
$b
)
{
global
$qa_sort_by_1
,
$qa_sort_by_2
;
$compare
=
qa_sort_cmp
(
$a
[
$qa_sort_by_1
],
$b
[
$qa_sort_by_1
]);
if
(
$compare
==
0
&&
$qa_sort_by_2
)
$compare
=
qa_sort_cmp
(
$a
[
$qa_sort_by_2
],
$b
[
$qa_sort_by_2
]);
return
$compare
;
}
/**
* General comparison function for two values, textual or numeric
*/
function
qa_sort_cmp
(
$a
,
$b
)
{
if
(
is_numeric
(
$a
)
&&
is_numeric
(
$b
))
// straight subtraction won't work for floating bits
return
$a
==
$b
?
0
:
(
$a
<
$b
?
-
1
:
1
);
else
return
strcasecmp
(
$a
,
$b
);
// doesn't do UTF-8 right but it will do for now
}
/**
* Inserts $addelements into $array, preserving their keys, before $beforekey in that array.
* If $beforekey cannot be found, the elements are appended at the end of the array.
*/
function
qa_array_insert
(
&
$array
,
$beforekey
,
$addelements
)
{
$newarray
=
array
();
$beforefound
=
false
;
foreach
(
$array
as
$key
=>
$element
)
{
if
(
$key
==
$beforekey
)
{
$beforefound
=
true
;
$qa_sort_by_1
=
$by1
;
$qa_sort_by_2
=
$by2
;
uasort
(
$array
,
'qa_sort_by_fn'
);
}
function
qa_sort_by_fn
(
$a
,
$b
)
/*
Function used in uasort to implement qa_sort_by()
*/
{
global
$qa_sort_by_1
,
$qa_sort_by_2
;
$compare
=
qa_sort_cmp
(
$a
[
$qa_sort_by_1
],
$b
[
$qa_sort_by_1
]);
if
((
$compare
==
0
)
&&
$qa_sort_by_2
)
$compare
=
qa_sort_cmp
(
$a
[
$qa_sort_by_2
],
$b
[
$qa_sort_by_2
]);
return
$compare
;
}
function
qa_sort_cmp
(
$a
,
$b
)
/*
General comparison function for two values, textual or numeric
*/
{
if
(
is_numeric
(
$a
)
&&
is_numeric
(
$b
))
// straight subtraction won't work for floating bits
return
(
$a
==
$b
)
?
0
:
((
$a
<
$b
)
?
-
1
:
1
);
else
return
strcasecmp
(
$a
,
$b
);
// doesn't do UTF-8 right but it will do for now
}
function
qa_array_insert
(
&
$array
,
$beforekey
,
$addelements
)
/*
Inserts $addelements into $array, preserving their keys, before $beforekey in that array.
If $beforekey cannot be found, the elements are appended at the end of the array.
*/
{
$newarray
=
array
();
$beforefound
=
false
;
foreach
(
$array
as
$key
=>
$element
)
{
if
(
$key
==
$beforekey
)
{
$beforefound
=
true
;
foreach
(
$addelements
as
$addkey
=>
$addelement
)
$newarray
[
$addkey
]
=
$addelement
;
}
$newarray
[
$key
]
=
$element
;
}
if
(
!
$beforefound
)
foreach
(
$addelements
as
$addkey
=>
$addelement
)
$newarray
[
$addkey
]
=
$addelement
;
$newarray
[
$addkey
]
=
$addelement
;
}
$
array
=
$newarray
;
$
newarray
[
$key
]
=
$element
;
}
if
(
!
$beforefound
)
{
foreach
(
$addelements
as
$addkey
=>
$addelement
)
$newarray
[
$addkey
]
=
$addelement
;
}
// Special values for the $beforekey parameter for qa_array_reorder() - use floats since these cannot be real keys
$array
=
$newarray
;
}
define
(
'QA_ARRAY_WITH_FIRST'
,
null
);
// collect the elements together in the position of the first one found
define
(
'QA_ARRAY_WITH_LAST'
,
0.6
);
// collect the elements together in the position of the last one found
define
(
'QA_ARRAY_AT_START'
,
0.1
);
// place all the elements at the start of the array
define
(
'QA_ARRAY_AT_END'
,
0.9
);
// place all the elements at the end of the array
function
qa_array_reorder
(
&
$array
,
$keys
,
$beforekey
=
null
,
$reorderrelative
=
true
)
/*
Moves all of the elements in $array whose keys are in the parameter $keys. They can be moved to before a specific
element by passing the key of that element in $beforekey (if $beforekey is not found, the elements are moved to the
end of the array). Any of the QA_ARRAY_* values defined above can also be passed in the $beforekey parameter.
If $reorderrelative is true, the relative ordering between the elements will also be set by the order in $keys.
*/
{
// Special values for the $beforekey parameter for qa_array_reorder() - use floats since these cannot be real keys
// Make a map for checking each key in $array against $keys and which gives their ordering
define
(
'QA_ARRAY_WITH_FIRST'
,
null
);
// collect the elements together in the position of the first one found
define
(
'QA_ARRAY_WITH_LAST'
,
0.6
);
// collect the elements together in the position of the last one found
define
(
'QA_ARRAY_AT_START'
,
0.1
);
// place all the elements at the start of the array
define
(
'QA_ARRAY_AT_END'
,
0.9
);
// place all the elements at the end of the array
$keyorder
=
array
();
$keyindex
=
0
;
foreach
(
$keys
as
$key
)
$keyorder
[
$key
]
=++
$keyindex
;
/**
* Moves all of the elements in $array whose keys are in the parameter $keys. They can be moved to before a specific
* element by passing the key of that element in $beforekey (if $beforekey is not found, the elements are moved to the
* end of the array). Any of the QA_ARRAY_* values defined above can also be passed in the $beforekey parameter.
* If $reorderrelative is true, the relative ordering between the elements will also be set by the order in $keys.
*/
function
qa_array_reorder
(
&
$array
,
$keys
,
$beforekey
=
null
,
$reorderrelative
=
true
)
{
//
Create the new key ordering in $newkeys
//
Make a map for checking each key in $array against $keys and which gives their ordering
$newkeys
=
array
();
$insertkeys
=
array
();
$offset
=
null
;
$keyorder
=
array
();
$keyindex
=
0
;
foreach
(
$keys
as
$key
)
$keyorder
[
$key
]
=
++
$keyindex
;
if
(
$beforekey
==
QA_ARRAY_AT_START
)
$offset
=
0
;
// Create the new key ordering in $newkeys
foreach
(
$array
as
$key
=>
$value
)
{
if
(
$beforekey
==
$key
)
$offset
=
count
(
$newkeys
)
;
$newkeys
=
array
();
$insertkeys
=
array
();
$offset
=
null
;
if
(
isset
(
$keyorder
[
$key
]))
{
if
(
$reorderrelative
)
$insertkeys
[
$keyorder
[
$key
]]
=
$key
;
// in order of $keys parameter
else
$insertkeys
[]
=
$key
;
// in order of original array
if
(
$beforekey
==
QA_ARRAY_AT_START
)
$offset
=
0
;
if
(
(
$beforekey
==
QA_ARRAY_WITH_LAST
)
||
((
$beforekey
===
QA_ARRAY_WITH_FIRST
)
&&
!
isset
(
$offset
))
)
$offset
=
count
(
$newkeys
);
foreach
(
$array
as
$key
=>
$value
)
{
if
(
$beforekey
==
$key
)
$offset
=
count
(
$newkeys
);
}
else
$newkeys
[]
=
$key
;
}
if
(
isset
(
$keyorder
[
$key
]))
{
if
(
$reorderrelative
)
$insertkeys
[
$keyorder
[
$key
]]
=
$key
;
// in order of $keys parameter
else
$insertkeys
[]
=
$key
;
// in order of original array
if
(
!
isset
(
$offset
))
// also good for QA_ARRAY_AT_END
$offset
=
count
(
$newkeys
);
if
(
$beforekey
==
QA_ARRAY_WITH_LAST
||
(
$beforekey
===
QA_ARRAY_WITH_FIRST
&&
!
isset
(
$offset
)))
$offset
=
count
(
$newkeys
);
if
(
$reorderrelative
)
ksort
(
$insertkeys
,
SORT_NUMERIC
);
// sort them based on position in $keys parameter
}
else
$newkeys
[]
=
$key
;
}
array_splice
(
$newkeys
,
$offset
,
0
,
$insertkeys
);
if
(
!
isset
(
$offset
))
// also good for QA_ARRAY_AT_END
$offset
=
count
(
$newkeys
);
// Rebuild the array based on the new key ordering
if
(
$reorderrelative
)
ksort
(
$insertkeys
,
SORT_NUMERIC
);
// sort them based on position in $keys parameter
$newarray
=
array
(
);
array_splice
(
$newkeys
,
$offset
,
0
,
$insertkeys
);
foreach
(
$newkeys
as
$key
)
$newarray
[
$key
]
=
$array
[
$key
];
// Rebuild the array based on the new key ordering
$array
=
$newarray
;
}
$newarray
=
array
();
foreach
(
$newkeys
as
$key
)
$newarray
[
$key
]
=
$array
[
$key
];
/*
Omit PHP closing tag to help avoid accidental output
*/
\ No newline at end of file
$array
=
$newarray
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment