G
Guest
I have created a small program that illustrates the problem. I would know how
to address the fields that I want to sort on in the greaterThan comparison.
Anybody who knows??
using System;
class Program
{
public List<item> itList, index1;
public class List<T>
{ T[] items;
public List(int capacity)
{ items = new T[capacity];
}
public T this[int index]
{ get { return items[index]; }
set { items[index] = value; }
}
static bool greaterThan(T a, T b)
{ return (a.x > b.x); // 1. How to do this?
} // 2. And a flexible assignment of x or y
public void swap(int i, int j)
{ T temp = this;
this = this[j];
this[j] = temp;
}
public void Bubblesort()
{ Boolean noswap = false;
while (!noswap)
{ noswap = true;
for (int i = 0; i < this.items.Length - 1; i++)
{ if (greaterThan(this, this[i + 1]))
{ this.swap(i, i + 1);
noswap = false;
}
}
}
}
}
public class item //struct or class whatever
{ public double x, y;
public item(double x, double y)
{ this.x = x;
this.y = y;
}
public double fieldx() {
return this.x;
}
}
static void Main(string[] args)
{ List<item> itList = new List<item>(99);
Random RandObj = new Random();
for (int i = 0; i < 99; i++)
{ itList = new item(RandObj.NextDouble() * 100,
RandObj.NextDouble() * 100);
}
List<item> index1 = new List<item>(99);
for (int i=0;i<99;i++){index1=itList;}
index1.Bubblesort();
}
}
to address the fields that I want to sort on in the greaterThan comparison.
Anybody who knows??
using System;
class Program
{
public List<item> itList, index1;
public class List<T>
{ T[] items;
public List(int capacity)
{ items = new T[capacity];
}
public T this[int index]
{ get { return items[index]; }
set { items[index] = value; }
}
static bool greaterThan(T a, T b)
{ return (a.x > b.x); // 1. How to do this?
} // 2. And a flexible assignment of x or y
public void swap(int i, int j)
{ T temp = this;
this = this[j];
this[j] = temp;
}
public void Bubblesort()
{ Boolean noswap = false;
while (!noswap)
{ noswap = true;
for (int i = 0; i < this.items.Length - 1; i++)
{ if (greaterThan(this, this[i + 1]))
{ this.swap(i, i + 1);
noswap = false;
}
}
}
}
}
public class item //struct or class whatever
{ public double x, y;
public item(double x, double y)
{ this.x = x;
this.y = y;
}
public double fieldx() {
return this.x;
}
}
static void Main(string[] args)
{ List<item> itList = new List<item>(99);
Random RandObj = new Random();
for (int i = 0; i < 99; i++)
{ itList = new item(RandObj.NextDouble() * 100,
RandObj.NextDouble() * 100);
}
List<item> index1 = new List<item>(99);
for (int i=0;i<99;i++){index1=itList;}
index1.Bubblesort();
}
}