Ruby: sort array of hashes, even though key may not exist
By : bsj
Date : March 29 2020, 07:55 AM
seems to work fine It depends what you want to do when a hash doesn't have sorting key. I can imagine two scenarios: 1) exclude the hash from sorting code :
arr.delete_if { |h| h[:key_to_sort].nil? }.sort_by { |h| h[:key_to_sort] }
arr.sort_by { |h| h[:key_to_sort] || REALLY_SMALL_OR_LARGE_VALUE }
|
if a key exist, sort it to be the last element of array
By : Swagg Bear
Date : March 29 2020, 07:55 AM
To fix this issue , Try this: code :
if (isset($ar['key']))
{
$temp = $ar['key'];
unset($ar['key']);
$ar['key'] = $temp;
}
|
Sort an array based on antoher and add key if not exist
By : Nilesh Patil
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Suppose $object is your original array, and $sort is the array with which you want to manipulate the original array, the solution would be like this: code :
function mergeArrays($array1, $array2){
$newArray = array();
foreach($array1 as $v){
if(!array_key_exists($v, $array2)){
$newArray[$v] = 0;
}else{
$newArray[$v] = $array2[$v];
}
}
return $newArray;
}
foreach($object as $key => $array){
$object[$key]['data'] = mergeArrays($sort, $array['data']);
}
// display $object array
var_dump($object);
|
Can't use sort() with HttpClient subscribe, property 'sort' does not exist on type 'Object'
By : devalnor
Date : March 29 2020, 07:55 AM
it helps some times I was able to get this to work by just specifying the type of res as [] code :
this.getConcept().subscribe((res:[]) => {
res.sort((f, n): number => {
if (f.code < n.code) return -1;
if (f.code > n.code) return 1;
return 0;
});
console.error(res);
this.arrConcept = res;
});
(res:IConcept[]) => { ... }
|
eXist-db collection sort
By : justincao84
Date : March 29 2020, 07:55 AM
Hope that helps It seems at the XQuery level you can change let $examples := $data/tei:TEI[@type="example"] to code :
let $examples := sort($data/tei:TEI[@type="example"], (), function($e) { $e/@xml:id })
let $examples := for $e in $data/tei:TEI[@type="example"] order by $e/@xml:id return $e
|