Implement a perhaps-C-looking sort predicate to sort strings and numbers 'properly'
git-svn-id: file:///home/vcs/svn/jsweeper/trunk@33 6611ac79-6612-48ef-a1e9-b906f853523emaster
parent
1baa932fc3
commit
cadf094319
|
@ -274,7 +274,30 @@ Port *PortManager::FindPort(jack_port_t *port)
|
|||
|
||||
static bool BaseSortPredicate(const Base *lhs, const Base *rhs)
|
||||
{
|
||||
return lhs->m_name < rhs->m_name;
|
||||
const char *a = lhs->m_name.c_str();
|
||||
const char *b = rhs->m_name.c_str();
|
||||
size_t len = std::max(lhs->m_name.length(), rhs->m_name.length());
|
||||
|
||||
for (size_t p = 0; p < len; p++) {
|
||||
char ac = (p < lhs->m_name.length()) ? a[p] : '\0';
|
||||
char bc = (p < rhs->m_name.length()) ? b[p] : '\0';
|
||||
|
||||
if (ac >= '0' && ac <= '9' && bc >= '0' && bc <= '9') {
|
||||
// Numeric sort
|
||||
long int av = strtol(&a[p], NULL, 0);
|
||||
long int bv = strtol(&b[p], NULL, 0);
|
||||
if (av != bv) {
|
||||
return av < bv;
|
||||
}
|
||||
} else {
|
||||
// Alphabetic sort
|
||||
if (ac != bc) {
|
||||
return ac < bc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void PortManager::Sort()
|
||||
|
|
Loading…
Reference in New Issue