+ Start a Discussion
Prady01Prady01 

Algorithm Help

Hi Forum.

     I am working on some development and I am facing few hicups with creating an algorithm for it. Please let me know if anyone has done this.

I have a list of object and this list contains object with duplicate values, I would like to eliminate the duplicates based on the Created date. Example imagine in the list I have two entity like this
Record1==> {obj.Name: Kat, Created Date: 6th july 2015}
Record2==>{obj.Name: Kat, Created Date: 7th july 2015}.
I would like to eliminate Record1 and keep Record2.

Many thanks
Prady01
 
Best Answer chosen by Prady01
Krishna SambarajuKrishna Sambaraju
If this List is created through a custom class you need to implement Comparable interface on that class and then sort the list in descending order of Created Date. Here is an example of implementing Comparable interface.
public class Employee implements Comparable{    
    public string employeeName;
    public date dateOfEmployment;
    public Employee(string name, Date empDate)
    {
        employeeName = name;
        dateOfEmployment = empDate;
    }
    public integer compareTo(object compareTo)
    {
        Employee emp = (Employee)compareTo;
        if (dateOfEmployment <= emp.dateOfEmployment) return 1;
        if (dateOfEmployment > emp.dateOfEmployment) return 0;
        return -1;
    }
}
Here is the example of using the above class to sort the records and eliminate duplicates.
List<Employee> employees = new List<Employee>();
employees.add(new Employee('Kat', Date.today().addDays(-2)));
employees.add(new Employee('Kat', Date.today()));
employees.sort();

Map<string, Employee> empMap = new Map<string, Employee>();
for (Employee emp : employees)
{
    if (empMap.get(emp.employeeName) == null)
    {
    	empMap.put(emp.employeeName, emp);
    }
}
system.debug('empMap : ' + empMap);
Hope this helps.

 

All Answers

Krishna SambarajuKrishna Sambaraju
If this List is created through a custom class you need to implement Comparable interface on that class and then sort the list in descending order of Created Date. Here is an example of implementing Comparable interface.
public class Employee implements Comparable{    
    public string employeeName;
    public date dateOfEmployment;
    public Employee(string name, Date empDate)
    {
        employeeName = name;
        dateOfEmployment = empDate;
    }
    public integer compareTo(object compareTo)
    {
        Employee emp = (Employee)compareTo;
        if (dateOfEmployment <= emp.dateOfEmployment) return 1;
        if (dateOfEmployment > emp.dateOfEmployment) return 0;
        return -1;
    }
}
Here is the example of using the above class to sort the records and eliminate duplicates.
List<Employee> employees = new List<Employee>();
employees.add(new Employee('Kat', Date.today().addDays(-2)));
employees.add(new Employee('Kat', Date.today()));
employees.sort();

Map<string, Employee> empMap = new Map<string, Employee>();
for (Employee emp : employees)
{
    if (empMap.get(emp.employeeName) == null)
    {
    	empMap.put(emp.employeeName, emp);
    }
}
system.debug('empMap : ' + empMap);
Hope this helps.

 
This was selected as the best answer
Prady01Prady01
Thank you Krishna Sambaraju, Why didnt I think of this this was easy anyhow the code was a cherry on the top.

Cheers
Prady01
Daniel MoresonDaniel Moreson
Hey,

Really, You are sharing wonderful information with examples about creating an algorithm.

Problems.com (https://problems.com/) is the best online place for those entrepreneurs who have problems related to startup or business.