# sort array by length and then alphabetically

Viewed   111 times

I'm trying to make a way to sort words first by length, then alphabetically.

``````// from
\$array = ["dog", "cat", "mouse", "elephant", "apple"];

// to
\$array = ["cat", "dog", "apple", "mouse", "elephant"];
``````

I've seen this answer, but it's in Java, and this answer, but it only deals with the sorting by length. I've tried sorting by length, using the code provided in the answer, and then sorting alphabetically, but then it sorts only alphabetically.

How can I sort it first by length, and then alphabetically?

5

You can put both of the conditions into a `usort` comparison function.

``````usort(\$array, function(\$a, \$b) {
return strlen(\$a) - strlen(\$b) ?: strcmp(\$a, \$b);
});
``````

The general strategy for sorting by multiple conditions is to write comparison expressions for each of the conditions that returns the appropriate return type of the comparison function (an integer, positive, negative, or zero depending on the result of the comparison), and evaluate them in order of your desired sort order, e.g. first length, then alphabetical.

If an expression evaluates to zero, then the two items are equal in terms of that comparison, and the next expression should be evaluated. If not, then the value of that expression can be returned as the value of the comparison function.

The other answer here appears to be implying that this comparison function does not return an integer greater than, less than, or equal to zero. It does.

Thursday, August 18, 2022

4

Do this for maintaining `\$array` in its original order

``````\$array = array(5,4,6,8,5,3,4,6,1);
\$sorted_array = \$array;
asort(\$sorted_array);
``````

Output

Saturday, October 1, 2022

5

You can create a custom sort method and use the http://www.php.net/manual/en/function.usort.php function to call it.

Example:

``````\$Collection = array(..); // An array of Genre objects

// Either you must make count a public variable, or create
// an accessor function to access it
function CollectionSort(\$a, \$b)
{
if (\$a->count == \$b->count)
{
return 0;
}
return (\$a->count < \$b->count) ? -1 : 1;
}

usort(\$Collection, "CollectionSort");
``````

If you'd like to make a more generic collection system you could try something like this

``````interface Sortable
{
public function GetSortField();
}

class Genre implements Sortable
{
private \$genre;
private \$count;

public function GetSortField()
{
return \$count;
}
}

class Collection
{
private \$Collection = array();

{
\$this->Collection[] = \$Item;
}

public function GetItems()
{
return \$this->Collection;
}

public function Sort()
{
usort(\$this->Collection, 'GenericCollectionSort');
}
}

function GenericCollectionSort(\$a, \$b)
{
if (\$a->GetSortField() == \$b->GetSortField())
{
return 0;
}
return (\$a->GetSortField() < \$b->GetSortField()) ? -1 : 1;
}

\$Collection = new Collection();
\$Collection->Sort();
\$SortedGenreArray = \$Collection->GetItems();
``````
Wednesday, October 12, 2022

3

Use the `usort` function with this comparison function:

``````function cmpByScore(\$a, \$b) {
if (\$a['score'] == \$b['score']) {
return 0;
}
return \$a['score'] > \$b['score'] ? 1 : -1;
}

usort(\$array, 'cmpByScore');
``````
Wednesday, September 7, 2022

5

As already has been mentioned, you need to allocate space for pointers, not chars:

``````char **array;
array = malloc(bufsize * sizeof(char*));
``````

Also you need to allocate space for separate lines, and copy lines to it:

``````while ((getline(&line, &bufsize, fp)) != -1) {
printf("%s", line);
array[i] = malloc(strlen(line) + 1);
strcpy(array[i], line);
i++;
}
``````

A mistake in your code is that all `array[i]` points to the same string in variable `line`, which is refilled by `getline()` every loop cycle.

It may be useful to read manual about getline. It permits allocation of strings by itself, but don't forget to use free().

``````array = NULL;
while ((getline(&array[i], &bufsize, fp)) != -1) {
printf("%s", array[i]);
array[i + 1] = NULL;
i++;
}
``````
Monday, October 31, 2022