Link to home
Start Free TrialLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

bubble sort logic help

I'm using a language,called APEX from Salesforce. I'm posting in this zone because I come from a .NET background and know C#. The logic in APEX is similar to C#, so I was hoping someone could identify the problem in my bubble sort logic that I'm 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.

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.

In my code, I'm returning 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.

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.

Does anyone see where I've gone wrong in my code?

Thanks for any help.
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;
			}
			
		}
		
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of s_chilkury
s_chilkury
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of -Dman100-

ASKER

Should the bubble sort increment a duplicate value by 1 if it is found?  That might be the problem I'm having.  If so, how could add that logic to correctly increment a duplicate value and get the list sorted?

Thanks for the help.