Implement natural-sort spaceship operator
This commit is contained in:
parent
97f323137c
commit
ba2a2cb654
|
@ -114,8 +114,8 @@ numerically_less (const char* a, const char* b)
|
||||||
return false; // equal
|
return false; // equal
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool
|
inline int
|
||||||
naturally_less (const char* a, const char* b)
|
natcmp (const char* a, const char* b)
|
||||||
{
|
{
|
||||||
const char* d_a = NULL;
|
const char* d_a = NULL;
|
||||||
const char* d_b = NULL;
|
const char* d_b = NULL;
|
||||||
|
@ -129,7 +129,7 @@ naturally_less (const char* a, const char* b)
|
||||||
const int ia = atoi (d_a);
|
const int ia = atoi (d_a);
|
||||||
const int ib = atoi (d_b);
|
const int ib = atoi (d_b);
|
||||||
if (ia != ib) {
|
if (ia != ib) {
|
||||||
return ia < ib;
|
return ia < ib ? -1 : 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
d_a = d_b = NULL;
|
d_a = d_b = NULL;
|
||||||
|
@ -146,25 +146,34 @@ naturally_less (const char* a, const char* b)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (*a == '_') {
|
if (*a == '_') {
|
||||||
return ' ' < *b;
|
return ' ' < *b ? -1 : 1;
|
||||||
} else if (*b == '_') {
|
} else if (*b == '_') {
|
||||||
return *a < ' ';
|
return *a < ' ' ? -1 : 1;
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
return *a < *b;
|
return *a < *b ? -1 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (d_a) {
|
if (d_a) {
|
||||||
return atoi (d_a) < atoi (d_b);
|
const int ia = atoi (d_a);
|
||||||
|
const int ib = atoi (d_b);
|
||||||
|
if (ia != ib) {
|
||||||
|
return ia < ib ? -1 : 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if we reach here, either strings are same length and equal
|
/* if we reach here, either strings are same length and equal
|
||||||
* or one is longer than the other.
|
* or one is longer than the other.
|
||||||
*/
|
*/
|
||||||
|
if (*a) { return 1; }
|
||||||
|
if (*b) { return -1; }
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (*a) { return false; }
|
inline bool
|
||||||
if (*b) { return true; }
|
naturally_less (const char* a, const char* b)
|
||||||
return false; // equal
|
{
|
||||||
|
return natcmp (a, b) < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace PBD
|
} // namespace PBD
|
||||||
|
|
Loading…
Reference in New Issue
Block a user