50٪ تخفیف روی تمام دوره‌ها!
پایان تخفیف تا:
مشاهده دوره‌ها
0

تبدیل کد مرتب سازی بر اساس چند خصوصیت جاوا به سی شارپ

سلام. کد مرتب سازی زیر ، در جاوا وجود دارد :


            if (activateTransactionMerging)
            {
                Collections.sort(dataset.getTransactions(), new Comparator(){
                @Override
                /**
				 * Compare two transactions
				 */
                public int compare(Transaction t1, Transaction t2)
                {
                    // we will compare the two transaction items by items starting
                    // from the last items.
                    int pos1 = t1.items.length - 1;
                    int pos2 = t2.items.length - 1;

                    // if the first transaction is smaller than the second one
                    if (t1.items.length < t2.items.length)
                    {
                        // while the current position in the first transaction is >0
                        while (pos1 >= 0)
                        {
                            int subtraction = t2.items[pos2] - t1.items[pos1];
                            if (subtraction != 0)
                            {
                                return subtraction;
                            }
                            pos1--;
                            pos2--;
                        }
                        // if they ware the same, they we compare based on length
                        return -1;

                        // else if the second transaction is smaller than the first one
                    }
                    else if (t1.items.length > t2.items.length)
                    {
                        // while the current position in the second transaction is >0
                        while (pos2 >= 0)
                        {
                            int subtraction = t2.items[pos2] - t1.items[pos1];
                            if (subtraction != 0)
                            {
                                return subtraction;
                            }
                            pos1--;
                            pos2--;
                        }
                        // if they are the same, they we compare based on length
                        return 1;

                    }
                    else
                    {
                        // else if both transactions have the same size
                        while (pos2 >= 0)
                        {
                            int subtraction = t2.items[pos2] - t1.items[pos1];
                            if (subtraction != 0)
                            {
                                return subtraction;
                            }
                            pos1--;
                            pos2--;
                        }
                        // if they ware the same, they we compare based on length
                        return 0;
                    }
                }

            });
            }


تا جایی که میدونم بر اساس تابع compare ، تراکنش ها رو مرتب می کنه. روش زیر در کد بالا هست. 

چه روشی برای پیاده سازی این تیکه کد زیر در سی شارپ هست ؟



Collections: IComparable
Collections.sort(dataset.getTransactions(), new Comparator(){ 
    Override 
    /** 
    * Compare two transactions 
    */ 
    public int compare(Transaction t1, Transaction t2) {
        /**
        * Codes 
        */ 
    } 
});
پرسیده شده در 1398/03/25 توسط

2 پاسخ

1

خیلی عالی. متشکرم. 

در ادامه فرمایشات آقای عادلی فر ، من با کد زیر ، تابع مرتب سازی را ، فرخوان کردم :


dataset.getTransactions().Sort(new Collection());
پاسخ در 1398/03/27 توسط
1

سلام به شما دوست عزیز.

برای این کار می توانید به صورت زیر عمل کنید.

   public class Transaction
{
public int[] items;
}

public class Collection : IComparer
{
public int Compare(Transaction x, Transaction y)
{
// we will compare the two transaction items by items starting
// from the last items.
int pos1 = x.items.Length - 1;
int pos2 = y.items.Length - 1;

// if the first transaction is smaller than the second one
if (x.items.Length < y.items.Length)
{
// while the current position in the first transaction is >0
while (pos1 >= 0)
{
int subtraction = x.items[pos2] - y.items[pos1];
if (subtraction != 0)
{
return subtraction;
}

pos1--;
pos2--;
}

// if they ware the same, they we compare based on length
return -1;

// else if the second transaction is smaller than the first one
}
else if (x.items.Length > y.items.Length)
{
// while the current position in the second transaction is >0
while (pos2 >= 0)
{
int subtraction = x.items[pos2] - y.items[pos1];
if (subtraction != 0)
{
return subtraction;
}

pos1--;
pos2--;
}

// if they are the same, they we compare based on length
return 1;
}
else
{
// else if both transactions have the same size
while (pos2 >= 0)
{
int subtraction = x.items[pos2] - y.items[pos1];
if (subtraction != 0)
{
return subtraction;
}

pos1--;
pos2--;
}

// if they ware the same, they we compare based on length
return 0;
}
}
}


class Program
{
static void Main(string[] args)
{
List transactions = new List();
transactions.Sort();
Console.ReadKey();
}
}

دقت کنید که کلاس transaction رو به صورت مثالی نوشتم و واقعی نیست ولی می تونید با استفاده از اینترفیس comparer و پیاده سازی متد اون همون کد جاوا رو می شه نوشت.

پاسخ در 1398/03/25 توسط

پاسخ شما