complex-ish sort on items in a dictionary


    Oct 26 2015 | 3:34 am
    Hi all, I have a dict which has the (simplified) hierarchy shown at the bottom of this post. I have a number of groups, each of which contain a number of items, each of which have the same fields. It's like a mega simple database. The data is very small (less than 5 groups, less than 5 items per group) but very dynamic (nearly all fields updating 30fps, occasionally new items added or removed).
    What i'd like to do is: * find the items which have a specific field set to 1 (e.g. data4 == 1) * sort those items according to a specific field (e.g. data3[0]) * iterate this sorted subset of items in order to do other things.
    I have no idea where to begin!
    I have a really ghetto idea which is: * iterate the dict and check each item if its data4 == 1 * if it is, concatenate the value I'd like to sort on (e.g. data3[0]) with the full path of the item, and add to an array (i.e. this array would be [5.2_group1::item2 3.45_group2::item1 7.3454_group2::item3] * sort the array with zl.sort * split each item in the array using '_' as a divider and lose the first part * I now have an array which is the full path of each item, sorted [group2::item1 group1::item2 group2::item3]
    I haven't implemented this yet, because it feels so horrible. Is there a better way?
    I can't put this in a matrix because the data is all different lengths and types, and even contains strings and other stuff.
    I was thinking perhaps I should go sqllite, but I'm worried about performance. I'm filling pretty much nearly all of the fields of this dict every frame at 30fps. Would that work with sqllite?
    //sample data structure: group1: ___item1 : ______data1: [value value] ______data2: [value value value] ______data3: value ______data4: 0 ___item2 : ______data1: [value value] ______data2: [value value value] ______data3: value ______data4: 1 group2: ___item1 : ______data1: [value value] ______data2: [value value value] ______data3: value ______data4: 1 ___item2 : ______data1: [value value] ______data2: [value value value] ______data3: value ______data4: 0 ___item3 : ______data1: [value value] ______data2: [value value value] ______data3: value ______data4: 1
    // this is what I'd like to add to the dict: sorted : [group2::item1 group1::item2 group2::item3]
    cheers

    • Oct 26 2015 | 7:54 pm
      If anyone comes across this post looking for a similar solution (sorting sub-dictionaries of a dictionary by a field), I ended up doing it all in javascript. was relatively straightforward (but i flattened the hierarchy, i.e. I don't have items inside groups anymore, instead the group name is prefixed to the item name. group1_item1, group2_item3 etc. I actually did this for other reasons, but it helped the javascript too).
      (EDIT: < code > tag doesn't work, using < pre >)
      /*
      Input: 
      	dictionary.
      
      Output:
      	array of keys sorted by 'x position' field, only if 'active' field is set
      
      */
      inlets=1;
      setinletassist(0, "dictionary");
      
      outlets=1;
      setoutletassist(0, "sorted array of keys");
      
      var thisname = "user_dict_sort.js" 
      var vrsn = 3;
      var verbose = false;
      
      thisname += " v" + vrsn + " ";
      
      post(thisname + " init"); post();
      
      //--------------------------------------------------
      function sort_by_pos(a, b) {
        if (a.pos < b.pos) return -1;
        if (a.pos > b.pos) return 1;
        return 0;
      }
      
      //--------------------------------------------------
      function setverbose(b) {
      	this.verbose = b;
      	post(thisname + "verbose: " + b);
      }
      
      //--------------------------------------------------
      function dictionary(dict_name) {
      	if(verbose) { post(thisname + "dictionary"); post(); }
      
      	var dict = new Dict(dict_name);  	// get master dictionary
      	var keys = dict.getkeys();			// get keys
      	var arr_to_sort = new Array();		// array of mini-dicts to be sorted
      	var arr_out = new Array();			// output array containing only keys sorted by pos
      	
      	// iterate keys
      	for(var i=0; i 0;
      		if(is_active) {
      			var pos = sub_dict.get("pos")[0];
      			arr_to_sort.push({ "key": key, "pos": pos });
      		}
      //		post(key, pos); post();
      	}
      
      	// sort
      	arr_to_sort.sort(sort_by_pos);
      	
      	// extract only keys and add to new array for output
      	for(var i=0; i jsobject conversion see
      https://cycling74.com/wiki/index.php?title=Parsing_Dict_files_to_Javascript_objects
      */