I hope this is what you are looking for.
I was just going to say look at [man]usort[/man] but I think what your asking for is a bit more complicated than just referencing some function. So I have included some sample code.
$wordarray = array();
while ($row = mysql_fetch_row($results)) {
$preword = explode('word', $row['textfield']);
$precount = strlen($preword[0]);
$wordarray[] = array($precount, $row['textfield']);
}
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
usort($wordarray, 'cmp');
foreach ($wordarray as $key => $value) {
echo $value[0] . ": " . $value[1] . "<br />";
}
Obviously, you first loop through the results from your database query so you can pull out the individual results so other operations can be done on them.
Second, you use [man]explode[/man] and [man]strlen[/man] to get the number of characters in front of the "word".
Third, you use a multidimensional array to store the [man]strlen[/man] value and the corresponding text field.
Fourth, you use [man]usort[/man] to sort by the first value of each array in the multidimensional array.
And finally you loop through the multidimensional array and output the values of the arrays in the multidimensional array.
I hope that help if you need more info check out [man]usort[/man] on php.net. Actually some of the code above is copied from there.