Best way to sort an array with the longest strings in the middle and the shortest at the beginning and end
By : mncrnich
Date : March 29 2020, 07:55 AM
it should still fix some issue Just for fun really. I was curious of the best way to do this. , Here is one way to do it (there's most likely a better solution): code :
function sort_length($str1, $str2)
{
if(strlen($str1) == strlen($str2))
{
return 0;
}
return strlen($str1) > strlen($str2) ? -1 : 1;
}
$words = array("Hello", "World", "this", "is", "a", "test");
usort($words, 'sort_length');
$new_list = array();
$boolean = true;
foreach($words as $word)
{
if($boolean)
{
array_push($new_list, $word);
}
else
{
array_unshift($new_list, $word);
}
$boolean = !$boolean;
}
//print_r($new_list);
|
How to sort the lines in a file from shortest to longest?
By : DatzZ Devesh
Date : March 29 2020, 07:55 AM
this one helps. Similar to Sorting lines from longest to shortest, how can I sort all of the lines in a file from shortest to longest? E.g." , It's almost exactely the same as in the link you gave code :
awk '{ print length($0) " " $0; }' $file | sort -n | cut -d ' ' -f 2-
|
Sort an array/table of words from shortest to longest
By : PINXUN0714陳品勛
Date : March 29 2020, 07:55 AM
Any of those help Corona/Lua how to sort a table of strings from shortest to longest , Assuming your table is a indexed table and not a keyed one try code :
test = {'123','1234','1245','1','12'}
table.sort(test, function(a,b) return #a<#b end)
for i,v in ipairs(test) do
print (i,v)
end
table.sort(test, function(a,b) return #a<#b end)
|
How to temporarily sort a file from longest to shortest line, before restoring it back to its original order?
By : Ram Awale
Date : March 29 2020, 07:55 AM
may help you . Done by enhancing your linked answer by these steps:
|
Sort a list with longest items first
By : Wckr
Date : March 29 2020, 07:55 AM
wish of those help I am using a lambda to modify the behaviour of sort. , Here is one way to do it:
|