The System.Collections.Generic namespace

  • Thread starter Thread starter fooshm
  • Start date Start date
F

fooshm

Hello,
I would like to implement the following code written in java using c#

java:

public void op (List myList) {
....
myList.add(myObjecy);
Collections.sort(myList);
}

The problem when trying to convert this type of code is that the sort
method is not located outside the class, so the only way (the way that
I see) to sort in c# is to declare a concrete class and not an
interface, i.e.

c#:
public void op (List<Object> myList) {
....
myList.add(myObjecy);
myList.sort();
}

Is there any solution to this problem?

Thanks,
Efi
 
Efi,

That's pretty much how you have to do it.

As a side note, your op is very inefficient. You are always resorting
the list after you add the item to the end. Instead of adding the item and
sorting it, you should call BinarySearch, and find the index that you should
add the item at, and then add it there.

Hope this helps.
 
Hi,
Thanks, I am just curious, what is reason behind this type of design?

Efi
Efi,

That's pretty much how you have to do it.

As a side note, your op is very inefficient. You are always resorting
the list after you add the item to the end. Instead of adding the item and
sorting it, you should call BinarySearch, and find the index that you should
add the item at, and then add it there.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello,
I would like to implement the following code written in java using c#

java:

public void op (List myList) {
...
myList.add(myObjecy);
Collections.sort(myList);
}

The problem when trying to convert this type of code is that the sort
method is not located outside the class, so the only way (the way that
I see) to sort in c# is to declare a concrete class and not an
interface, i.e.

c#:
public void op (List<Object> myList) {
...
myList.add(myObjecy);
myList.sort();
}

Is there any solution to this problem?

Thanks,
Efi
 
Thanks, I am just curious, what is reason behind this type of design?

Sorting is a O(n * log(n)) operation. That is, the time spent sorting is
proportional to the length of the list times the log of the number of
items in the list. Adding at the end is amortized to O(1), BTW.

BinarySearch is a O(log(n)) operation. Inserting into the middle of a
list is a O(n) operation. The dominant term is n, so on the whole it's
O(n).

As you can probably see, the larger n gets, the more expensive adding
and sorting is than inserting.

-- Barry
 
Hi,
My question wasn't about the performance.
Why C# is not using the same design as java i.e. a sort function
outside the concrete class.

Thanks,
Efi
Barry Kelly כתב:
 
Why C# is not using the same design as java i.e. a sort function
outside the concrete class.

I don't know. Here are two possible reasons:

* To make sorting more discoverable - it's easier for beginners to find
instance methods than static methods on different, utility classes.

* To make sorting more efficient, since an instance method can sort the
internal array rather than going through interface dispatch for each
element access.

-- Barry
 
A partial solution is to embrace generics:

public void op <T>(List<T> myList)
{ ...
myList.add(myObjecy);
myList.sort();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top