Although I wouldn't recommend it (given the relative simplicity of piping the result through an external sort command) you can do this at least with recent versions of GNU awk (at least 4.0 IIRC), as described at Sorting Array Values and Indices with gawk
Here's how you could implement it, assuming you have the data in an associative array in which the index is Firstname Lastname. First you need to define a custom comparison function that splits the index, compares first on Lastname then (as a tie breaker) on Firstname e.g.
function mycmp(ia, va, ib, vb, sa, sb) {
if(split(toupper(ia), sa) && split(toupper(ib), sb)) {
if(sa[2] < sb[2]) return -1;
else if (sa[2] > sb[2]) return 1;
else {
# compare first names
if(sa[1] < sb[1]) return -1;
else if (sa[1] > sb[1]) return 1;
else return 0;
}
}
else return 0;
}
Now you can use the PROCINFO["sorted_in"] array sorting method mentioned in comments by @zwets
PROCINFO["sorted_in"] = "mycmp";
for(i in a) print i, a[i];
Putting it together
#!/usr/bin/gawk -f
function mycmp(ia, va, ib, vb, sa, sb) {
if(split(toupper(ia), sa) && split(toupper(ib), sb)) {
if(sa[2] < sb[2]) return -1;
else if (sa[2] > sb[2]) return 1;
else {
# compare first names
if(sa[1] < sb[1]) return -1;
else if (sa[1] > sb[1]) return 1;
else return 0;
}
}
else return 0;
}
{
a[$1" "$2] = $3;
}
END {
PROCINFO["sorted_in"] = "mycmp";
for(i in a) print i, a[i];
}
Testing:
$ ./namesort.awk yourfile
Kobe Bryant 24
Kevin Durant 35
Blake Griffin 32
Lebron James 23
Dikembe Mutumbo 55
In lesser or older versions of awk, your best bet may be to store the data indexed by Lastname Firstname instead, sort with the conventional asorti, then split and swap the fields of the indices as you traverse the array to print it:
awk '
{a[$2" "$1]=$3}
END {
n=asorti(a,b); for (i=1;i<=n;i++) {split(b[i],s); print s[2], s[1], a[b[i]]}
}' yourfile