digitalmars.D - sorting arrays of strings, deleting items from array...
- Bill Lear (22/22) Jan 22 2007 I have a map of sha1 values to file names, thus:
- Kevin Bealer (21/54) Jan 25 2007 What I would do is this:
I have a map of sha1 values to file names, thus: char[][][char[]] sha_map; I would like to loop over the values of the map, and print the file names that are duplicates, sort of like this: foreach (key; sha_map.keys) { auto file_names = sha_map[key]; // if only one file name, it is unique, so skip it if (file_names.length <= 1) continue; // sort file names by criteria that place the file // to retain at the front of the array. sort(file_names, retain_criteria_comparator()); foreach (i, file_name; file_names) { if (i == 0) continue; writefln(file_name); } } But I'm at a loss how to do the sort I would like. Any advice would be appreciated. Bill -- r * e * * o * y * a * c * m * a * l * z * p * r * . * o *
Jan 22 2007
Bill Lear wrote:I have a map of sha1 values to file names, thus: char[][][char[]] sha_map; I would like to loop over the values of the map, and print the file names that are duplicates, sort of like this: foreach (key; sha_map.keys) { auto file_names = sha_map[key]; // if only one file name, it is unique, so skip it if (file_names.length <= 1) continue; // sort file names by criteria that place the file // to retain at the front of the array. sort(file_names, retain_criteria_comparator()); foreach (i, file_name; file_names) { if (i == 0) continue; writefln(file_name); } } But I'm at a loss how to do the sort I would like. Any advice would be appreciated. Bill -- r * e * * o * y * a * c * m * a * l * z * p * r * . * o *What I would do is this: struct sort_fname { char[] fname; int opCmp(sort_fname other) { // or whatever return other.fname.length > fname; } } Now, use the type sort_fname in place of char[]: char[][][char[]] sha_map; becomes sorted_fname[][char[]] sha_map; You can pick whatever criteria you like. Because this is a struct, you can use "sorted_fname" in the opCmp(). If it was a class, you'd need to use "int opCmp(Object other)", and cast 'other' to the right type to verify that its the right kind of class. In this case struct is easier. The sort just looks like: file_names.sort; Kevin
Jan 25 2007