fix list sorting

This commit is contained in:
taiyu 2015-11-24 00:30:02 -08:00
parent cb11364552
commit 9d50f88cef
3 changed files with 22 additions and 14 deletions

View file

@ -50,8 +50,14 @@ void list_cat(list_t *list, list_t *source) {
} }
} }
// pass the pointer of the object we care about to the comparison function
static int list_cmp(const void *l, const void *r, void *_cmp) {
int (*cmp)(const void *, const void *) = _cmp;
return cmp(*(void**)l, *(void**)r);
}
void list_sort(list_t *list, int compare(const void *left, const void *right)) { void list_sort(list_t *list, int compare(const void *left, const void *right)) {
qsort(list->items, list->length, sizeof(void *), compare); qsort_r(list->items, list->length, sizeof(void *), list_cmp, compare);
} }
int list_seq_find(list_t *list, int compare(const void *item, const void *data), const void *data) { int list_seq_find(list_t *list, int compare(const void *item, const void *data), const void *data) {

View file

@ -1128,9 +1128,9 @@ static struct cmd_results *cmd_scratchpad(int argc, char **argv) {
// sort in order of longest->shortest // sort in order of longest->shortest
static int compare_set(const void *_l, const void *_r) { static int compare_set(const void *_l, const void *_r) {
struct sway_variable * const *l = _l; struct sway_variable const *l = _l;
struct sway_variable * const *r = _r; struct sway_variable const *r = _r;
return strlen((*r)->name) - strlen((*l)->name); return strlen(r->name) - strlen(l->name);
} }
static struct cmd_results *cmd_set(int argc, char **argv) { static struct cmd_results *cmd_set(int argc, char **argv) {

View file

@ -387,18 +387,20 @@ int workspace_output_cmp_workspace(const void *a, const void *b) {
int sway_binding_cmp_keys(const void *a, const void *b) { int sway_binding_cmp_keys(const void *a, const void *b) {
const struct sway_binding *binda = a, *bindb = b; const struct sway_binding *binda = a, *bindb = b;
if (binda->modifiers > bindb->modifiers) { // Count keys pressed for this binding. important so we check long before
return 1; // short ones. for example mod+a+b before mod+a
} else if (binda->modifiers < bindb->modifiers) { unsigned int moda = 0, modb = 0, i;
return -1;
} // Count how any modifiers are pressed
for (i = 0; i < 8 * sizeof(binda->modifiers); ++i) {
if (binda->keys->length > bindb->keys->length) { moda += (binda->modifiers & 1 << i) != 0;
return 1; modb += (bindb->modifiers & 1 << i) != 0;
} else if (binda->keys->length < bindb->keys->length) { }
return -1; if (bindb->keys->length + modb != binda->keys->length + moda) {
return (bindb->keys->length + modb) - (binda->keys->length + moda);
} }
// Otherwise compare keys
for (int i = 0; i < binda->keys->length; i++) { for (int i = 0; i < binda->keys->length; i++) {
xkb_keysym_t *ka = binda->keys->items[i], xkb_keysym_t *ka = binda->keys->items[i],
*kb = bindb->keys->items[i]; *kb = bindb->keys->items[i];