- #1
ktoz
- 171
- 12
Hi
I have a sorting problem I haven't been able to solve and was hoping someone here could lend some insights.
The basic problem is resorting pages in a page layout application (QuarkXPress) where I'm constrained to only the functions Quark supplies for moving pages around in a document. Basically all I can do is place a page before or after another page. I have no access to the internal data structure so can't just grab the page array and swap pointers.
The difficulty arises when I perform an insert before/after a page, the indexes for everything after the insert changes which requires recomputing the entire array (at least in my very inefficient efforts thus far) and I'd like to come up with something a bit more elegant.
Here is the problem space
- Quark native page array with no access to it's elements or structure. The only known about this array is that pages are accessed by one based index using an accessor function.
- An unordered associative array (an Object-C http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html") where array keys are the unique page id stored in a database. The database and NSDictionary are mine and are not part of Quark. I use them to provide direct access to a page without having to search the entire array in a loop.
- The only Quark manipulation functions available are insert before/insert after.
- I would like any solution to use only one of the insert before/insert after functions so as to keep it simple and automatic.
With those in mind, here is a concrete example:
Say there is an array of 4 pages (1 thru 4) sorted correctly and the user renumbers page 2 to page 10
User renumbers page 2
Since the NSDictionary is mine, I'm free to do key sorts using standard sorting functions and can use this array as reference for sorting the Quark pages. Keep in mind that the actual NSDictionary itself is not resorted though. I can create arrays of sorted keys, but for efficiency sake, the dictionary preserves it's initial order.
Now using only "insert before" or "insert after" (but not both) resort the page array to match the order found in the key array.
This may actually be relatively easy but I've developed a brain block and haven't been able to figure it out. Any help greatly appreciated
I have a sorting problem I haven't been able to solve and was hoping someone here could lend some insights.
The basic problem is resorting pages in a page layout application (QuarkXPress) where I'm constrained to only the functions Quark supplies for moving pages around in a document. Basically all I can do is place a page before or after another page. I have no access to the internal data structure so can't just grab the page array and swap pointers.
The difficulty arises when I perform an insert before/after a page, the indexes for everything after the insert changes which requires recomputing the entire array (at least in my very inefficient efforts thus far) and I'd like to come up with something a bit more elegant.
Here is the problem space
- Quark native page array with no access to it's elements or structure. The only known about this array is that pages are accessed by one based index using an accessor function.
- An unordered associative array (an Object-C http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html") where array keys are the unique page id stored in a database. The database and NSDictionary are mine and are not part of Quark. I use them to provide direct access to a page without having to search the entire array in a loop.
- The only Quark manipulation functions available are insert before/insert after.
- I would like any solution to use only one of the insert before/insert after functions so as to keep it simple and automatic.
With those in mind, here is a concrete example:
Say there is an array of 4 pages (1 thru 4) sorted correctly and the user renumbers page 2 to page 10
Code:
initial page sort:
[1, 2, 3, 4]
initial associative array (NSDictionary) :
{
754: {
id: 754,
number: 1,
ads: {},
images: {},
stories: {}
},
755: {
id: 755,
number: 2,
ads: {},
images: {},
stories: {}
},
756: {
id: 756,
number: 3,
ads: {},
images: {},
stories: {}
},
757: {
id: 757,
number: 4,
ads: {},
images: {},
stories: {}
}
}
User renumbers page 2
Code:
Quark order before resort: [1, [COLOR="red"]10[/COLOR], 3, 4]
NSDictionary after renumbering:
{
754: {
id: 754,
number: 1,
ads: {},
images: {},
stories: {}
},
755: {
id: 755,
[COLOR="Red"]number: 10,[/COLOR]
ads: {},
images: {},
stories: {}
},
756: {
id: 756,
number: 3,
ads: {},
images: {},
stories: {}
},
757: {
id: 757,
number: 4,
ads: {},
images: {},
stories: {}
}
}
Sorted key array:
[754, 756, 757, [COLOR="Red"]755[/COLOR]]
Since the NSDictionary is mine, I'm free to do key sorts using standard sorting functions and can use this array as reference for sorting the Quark pages. Keep in mind that the actual NSDictionary itself is not resorted though. I can create arrays of sorted keys, but for efficiency sake, the dictionary preserves it's initial order.
Now using only "insert before" or "insert after" (but not both) resort the page array to match the order found in the key array.
This may actually be relatively easy but I've developed a brain block and haven't been able to figure it out. Any help greatly appreciated
Last edited by a moderator: