in reply to Re: Twisted sort requirements
in thread Twisted sort requirements
I guess that might not be possible with a simple sort call.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Twisted sort requirements
by davido (Cardinal) on Feb 01, 2006 at 07:35 UTC | |
It is definately possible. graff kindly /msg'ed me to let me know I missed a specification. Update: I have now re-written my original method in such a way that the author is stored for future comparisons if title is "THIS BOOK FIRST". I then rewrote the logic into basic 'if' conditionals so that I could visualize the flow a little better. But I guess I've stared at it too long, and am simply missing something that someone's going to spot quickly. Unfortunately, that "something" is eluding me. Here is my code so far, that doesn't result in the correct sort order. I hope someone spots my flaw because it's going to keep me awake tonight.
I know it's really ugly. The "if" statements tend to do that, but I converted over to "if"s to help in visualizing the flow. ...obviously it didn't help me see the problem. I can't wait to hear if anyone else can spot it. Dave | [reply] [d/l] |
by davido (Cardinal) on Feb 01, 2006 at 08:31 UTC | |
I'm choosing to follow up to my own node, because I believe the lesson learned is worth repeating for the benefit of others who may find the pitfall that tripped me up. The problem with my above code is that $author doesn't get set right away; it takes a few sort comparison iterations before $author gets set, and by then the order has already began to take shape. This resulted in one of the "lisa" entries being stranded near the bottom of the list, when it needed to find its way up to the top of the list. The solution is (as BrowserUk already pointed out further down in this thread, but which I errantly believed was unnecessary) to predetermine who the key author is, before starting the sort routine. The following code does finally work, and is ready to be tidied up into a more Perlish format:
And here is the sort routine re-written with the same logic but in what some might consider a more familiar sort idiom:
I love this question. ++ to the OP. Thanks for puzzler, seriously! Dave | [reply] [d/l] [select] |
by graff (Chancellor) on Feb 01, 2006 at 08:41 UTC | |
I'll confess that I've forgotten most of the (relatively few) details I've ever known about sorting implementations, but I have a strong hunch that changing the sort criteria upon seeing input item N (where N is something other than "the first") would violate an essential underlying assumption that is built into most sort algorithms. | [reply] |