Results 1 to 9 of 9
Thread: implementing a sorting feature
- 01-12-2011, 02:22 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
implementing a sorting feature
I'm using a derivative of the Java language,called APEX from Salesforce. It is similar to the Java language, so I was hoping I might find some help on this forum and get some guidance on the logic I am trying to implement.
I'm trying to re-sort a list when a new record is added or an existing record is updated.
For example, when a record is created, the field is given a sort order number by the user. Assuming we have 10 records, with the sort order numbers going from 1 - 10, I want to re-sort when an existing record is updated or new record is added.
Task Name ----------- Sort Order
Task 1 1
Task 2 2
Task 3 3
Task 4 4
Task 5 5
Task 6 6
Task 7 7
Task 8 8
Task 9 9
Task 10 10
So, if a new Task is added "Task 11" but is given a sort order of 5 then I need to adjust all the above reocrds so that Task 5 has a sort number of 6, Task 6 has a sort number of 7, Task 7 has a sort number of 8, etc. I just need to re-sort the list based on how the sort numbers are changed.
In my code, I'm querying a list to return a list of Tasks that are back logged, these are the only records that apply to be re-sorted. So, I need to loop over that list and re-sort.
List<Project_Management_Task_List__c> lstBacklog = [Select Sort_Order__c From Project_Management_Task_List__c Where Status__c = 'Backlog'];
I was looking into insertion sort, bubble sort and selection sort algorithms, but I couldn't get it to work correctly.
Any help would be greatly appreciated.
Thanks.
- 01-12-2011, 03:10 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
If you are adding/removing/changing orders frequently, it might be more optimal to create a Tree structure that naturally sorts the Tasks rather than sorting every time a change occurs. Hard to give advice on any sorting algorithms that "don't work" without code, java or not.
- 01-12-2011, 03:39 AM #3
I agree with doWhile - if you are considering resorting a list on every insert, you should really consider a data structure other than a list - such as a balanced binary tree or at the very least a regular binary search tree. In the worst case, its no worse than a list, and in the best case, it'll reduce overhead to log2(n). Insertions into a binary tree are much faster than inserting into a list and resorting.
Using an advanced structure like a RedBlack tree or the like can really improve performance in the average case, but might be more than you want to bite off and chew.
- 01-12-2011, 03:41 AM #4
Oh yeah, also, if you are defining a task name and a separate sort value, you might consider something as simple as a HashMap - using the sort order as the key, and the task as the value.
- 01-12-2011, 04:42 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
Thanks for the replies. Unfortunately, there are limitations wihin the language I'm using, even though it is similar to Java, it doesn't have a HashMap or a TreeSet.
The above code didn't work. I was hoping I might find an example or how to implement the logic.Java Code:List<Project_Management_Task_List__c> lstPM = [Select Sort_Order__c From Project_Management_Task_List__c Where Id in: trigger.new AND Status__c = 'Backlog']; for (Integer i = 1; i < lstPM.size(); i++) { Integer a = Integer.valueOf(lstPM[i].Sort_Order__c); Integer j; for (j = i - 1; j >= 0 && lstPM[j].Sort_Order__c > a; j--) { lstPM[j + 1].Sort_Order__c = lstPM[j].Sort_Order__c; } lstPM[j + 1].Sort_Order__c = a; }
Thanks for your help though. I sincerely appreciate it.
Regards.
- 01-12-2011, 01:32 PM #6
And what is stopping you from implementing one? Java is one of the few languages that has a class library, most languages require that you or someone implement all the structures manually....it doesn't have a HashMap or a TreeSet.
Looking at your algorithm still makes makes me stick with my previous post, I think you need to consider the way in which data is being stored, and it will simplify the sorting process.
- 01-12-2011, 04:54 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
I could implement a HashMap or TreeSet I suppose, but I would have to develop it since I'm using the APEX rather than utilizing the class library Java provides.
The lists I'm working with are small. It would be unusual for the list size to be larger than 25 records total. A list size of 50 would be highly unlikely and a list size of 100 would be unheard of.
Considering the list size is so small, I was trying to implement a bubble sort last night. I'm not getting any errors, but the logic in my code isn't quite right.
See the code below. My list size is 2 in the test I'm running with both sort order values set at 1. So, it should update the record sort order number to 2 for the record I'm not editing to move it below the record I am editing with the sort order number of 1.
Do you see where I've gone wrong in my code?
Java Code:trigger ProjectManagementTaskListBefore on Project_Management_Task_List__c (before insert, before update) { List<Project_Management_Task_List__c> lstBacklog = [Select Sort_Order__c From Project_Management_Task_List__c Where Status__c = 'Backlog']; Boolean swapped = true; Integer j = 0; Integer tmp; while(swapped) { //system.debug('We are here'); swapped = false; j++; system.debug('List size is = ' + lstBacklog.size()); for(Integer i=0; i < lstBacklog.size() - j; i++) { //system.debug('We are in the outer loop here'); system.debug('Sort Order Values = ' + lstBacklog[i].Sort_Order__c); if (lstBacklog[i].Sort_Order__c > lstBacklog[i+1].Sort_Order__c) { system.debug('We are in the if condition here'); tmp = lstBacklog[i].Sort_Order__c.intValue(); system.debug('Tmp = ' + tmp); lstBacklog[i].Sort_Order__c = lstBacklog[i+1].Sort_Order__c; system.debug('Sort Order Number = ' + lstBacklog[i].Sort_Order__c); lstBacklog[i+1].Sort_Order__c = tmp; swapped = true; } } } }
- 01-12-2011, 06:25 PM #8
I think the logic is ok - the only thing you are changing is the Sort_Order though, not the actual position of the elements in the array. Is that what you want to do? It seems that this wouldn't actually do anything.
I would think that you would compare the sort order fields, and then swap the actual elements in the array like this pseudocode:
Does that make sense? right now you are swapping only the sort order of an item, not the item itself. My code uses the sort order comparison to swap the item itself.Java Code:... Object[] list is some list of Objects outer loop inner loop if list[i].sortOrder > list[i+1].sortOrder Object tmp = list[i] list[i] = list[i+1] list[i+1] = tmp; end if end inner loop end outer loop
- 01-12-2011, 07:08 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
I swapped my code using your example, but it still isn't executing correctly. I'm not getting any errors, but as you pointed out, it isn't doing anything.
What should happen, is the position of the elements should get swapped, but also, if there are duplicate values in the array, then it needs to take the element value that is a duplicate and increment that by one and continue that process until there are no duplicate values and the values are all sorted.
I think I misinterpreted the bubble sort because it appears it is only sorting the element values.
Here is the modified code:
Java Code:trigger ProjectManagementTaskListBefore on Project_Management_Task_List__c (before insert, before update) { List<Project_Management_Task_List__c> lstBacklog = [Select Sort_Order__c From Project_Management_Task_List__c Where Status__c = 'Backlog']; Boolean swapped = true; Integer j = 0; Project_Management_Task_List__c pmtl; while(swapped) { swapped = false; j++; for(Integer i=0; i < lstBacklog.size() - j; i++) { if (lstBacklog[i].Sort_Order__c > lstBacklog[i+1].Sort_Order__c) { pmtl = lstBacklog[i]; lstBacklog[i] = lstBacklog[i+1]; lstBacklog[i+1] = pmtl; swapped = true; } } } }
Similar Threads
-
possible feature of jdk1.7
By sikandar in forum Advanced JavaReplies: 2Last Post: 09-05-2008, 07:21 AM -
Spell check feature
By ravian in forum Advanced JavaReplies: 2Last Post: 12-27-2007, 09:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks