Sorting on a custom field

*********SORT EngineRecordList ON CustomField **************

@functions{

System.Collections.Generic.List<EngineRecord> recList(EngineRecordList rlist){
    System.Collections.Generic.List<EngineRecord> l = new System.Collections.Generic.List<EngineRecord>();

    foreach(EngineRecord r in rlist){
        l.Add(r);
    }


    l.Sort(delegate(EngineRecord x, EngineRecord y){
        	if (x.GetRecordList("People").Count == 0 && y.GetRecordList("People").Count== 0) return 0;
        
	else if(x.IsDBNull("People") || x.GetRecordList("People").Count == 0) return -1;
        
	else if(y.IsDBNull("People") || y.GetRecordList("People").Count == 0) return 1;
        
	else if (x.GetRecordList("People")[0].GetString("LastName") == "") return -1;
        
	else if (y.GetRecordList("People")[0].GetString("LastName") == "") return 1;
        
	else if (y.GetRecordList("People")[0].GetString("LastName") == x.GetRecordList("People")[0].GetString("LastName")) return x.GetRecordList("People")[0].GetString("FirstName").CompareTo(y.GetRecordList("People")[0].GetString("FirstName"));
        
	else return x.GetRecordList("People")[0].GetString("LastName").CompareTo(y.GetRecordList("People")[0].GetString("LastName"));
    });
            
    // l.Reverse();
    return l;
  
}

}