libplist icon indicating copy to clipboard operation
libplist copied to clipboard

It is recommended to add a function void dict_sort(plist_t pl)

Open xdeng opened this issue 2 years ago • 0 comments

typedef struct _psi{
	string key;
	plist_t item;
}psi;

bool compare_psi(const psi &first, const psi &second)
{
	return first.key < second.key;
}

void dict_sort(plist_t pl)
{
	if(plist_get_node_type(pl) != PLIST_DICT)
		return;

	list<psi> ls;
	plist_dict_iter iter = NULL;
	plist_dict_new_iter(pl, &iter);
	if (iter) {
		char *key = NULL;
		plist_t node = NULL;
		plist_dict_next_item(pl, iter, &key, &node);
		while (node) {
			plist_type nt = plist_get_node_type(node);
			if(nt == PLIST_DICT)
				dict_sort(node);
			psi i;
			i.key = key;
			i.item = plist_copy(node);
			ls.push_front(i);
			free(key);
			plist_dict_next_item(pl, iter, &key, &node);
		}
		free(iter);
	}

	ls.sort(compare_psi);

	list<psi>::iterator it;
	for (it = ls.begin(); it != ls.end(); ++it)
	{
		plist_dict_remove_item(pl, it->key.c_str());
		plist_dict_set_item(pl, it->key.c_str(), it->item);
	}
}

similar to this

xdeng avatar Jun 10 '22 10:06 xdeng