Click here to Skip to main content
15,886,422 members
Everything / General Programming / Sorting

Sorting

sorting

Great Reads

by Christoph Buenger
Describes PHP application development with the free Scavix Web Development Framework (Scavix-WDF).
by Member 9294701
A simple, portable yet efficient Quicksort implementation in C
by brunofer2007
Easy way to sort nodes in a TreeView using a recursive function.
by DotNetLead.com
How to implement paging and sorting to yield good performance

Latest Articles

by Perić Željko
Sorting Multi-Dimensional Arrays in C# with QuickSort Sort Extensions
by Ramesh Bevara
An overview of a helper class to build dynamic order by clause in LINQ query in C#
by spore123
This is a fast multi-threaded quick-sort based algorithm
by The Sun God
Comparison of the various sorting algorithms on the basis of several factors

All Articles

Sort by Title

Sorting 

12 May 2014 by Brad Joss
[RegSave] your property to the Registry, and more.
7 Dec 2009 by nGrafix
Hi giys,i wrote my programme in VS.NET 2008 that takes real numbers separated by commas from TextBox1.Text, sort them and display the sorted in TextBox2.Text.Now i want to add another 2 sorting algorithm that uses the same TexBoxes with different buttons to perform the sorting.I need...
7 Dec 2009 by AspDotNetDev
This seems like a homework question...What do you need help with specifically? Sorting algorithms are well documented online. Have you searched online yet? Wikipedia has some good information. Once you've done that, then come here with specific questions and we'll be happy to assist (so long...
28 Dec 2013 by log988
I have a file with 5 million records which are include of numbersthat they are out of sequence (irregular)you can find file structure below :for instance desired Result------------ --------------- 723,80 1,4 14,50 1,5 723,2 ...
20 Dec 2013 by CPallini
Quote:I didn't use any tecniques such as linq, ....I want to do it with available algorithms and arrange the file.One of best sorting algorithms is quicksort[^]. Happy coding.
20 Dec 2013 by PIEBALDconsult
The DOS SORT utility appears to sort that as specified; have you tried it?Or is this homework?
23 Dec 2013 by Matt T Heffron
If the whole file would fit in memory, I'd read it all in with File.ReadAllLines().Make 1 pass through all of the lines and build a parallel array that has the parsed numeric keys.Then use Array.Sort(TKey[] keys, TValue[] items) to do the sorting.(This will sort both arrays...
23 Dec 2013 by Maciej Los
5 million records? Do you want to torture yourself using flat file to store large amount of data?Firstly, i would suggest to change the way you store the data. But in a meanwhile - use OleDb. It should be the easiest and the quickest way to sort data. Please see:Other Text File Driver...
23 Dec 2013 by log988
I wrote a code that will sort based on decimals. The quick sort algorithm. But my issue is the removal of the number 0 in some decimals. Eg 723.1000 becomes 723.1 Numbers are important to me because I want to be displayed and stored asQuote:@Quote:Matt T HeffronFrom the image, what you want...
9 Feb 2016 by User 12277010
Trying to create a method that returns a query so I don't have to LINQ sort every time I need the results. LINQ code:public IEnumerable queryReturn(){ string[] tr = staticGetTRdata(); char[] splitChars = new char[] { ',' }; var query = from data in tr ...
9 Feb 2016 by F-ES Sitecore
Anonymous types can't really be used outside the function they are created in. Create a concrete class with the properties you need and return an IEnumerable of that class instead.
29 Oct 2011 by Trent Tobler
A memory based BTree ICollection and IDictionary implementation.
18 Jan 2013 by Andrey Sitaev
Here I placed source files, binaries and brief instructions for FastGrid control.
4 Apr 2013 by Michael Mann (mann.DEV)
Plugin migration with new features of .NET 3.5.
13 Oct 2013 by Member 9294701
A simple, portable yet efficient Quicksort implementation in C
12 Dec 2014 by Inimicos
Please help me describe this code.public int CompareTo(object obj){if (obj is Person){Person otherPerson = obj as Person;return this.Age - otherPerson.Age;}else{throw new ArgumentException("Object to compare to is not a Person object.");}}in particular i intresting.What...
12 Dec 2014 by DamithSL
// input obj cast to Person Person otherPerson = obj as Person;// return the difference of Age values of both comparing objects return this.Age - otherPerson.Age;Read the documentation of IComparable Interface[^] CompareTo method should return integer value based on the comparison, here...
12 Dec 2014 by OriginalGriff
The as operator checks the given object and if it can be converted to the given type returns it as that type. Otherwise, it returns null. It's used when you don't know what type of value is being passed into a method (often an event handler) and you need to check so that you don't cause problems...
15 Dec 2010 by hector [.j.] rivas
Helping the combo box autocomplete function with a frequency count.
25 Mar 2014 by Member 10347014
I am a beginner, I am making an app which will recommend meal to the user on the basis of his previously entered meal details.I need to give 4 types of recommendation to the user:The meal he had most.The meal he had less.The meal he had most on that day.The meal he had most on that...
25 Mar 2014 by CHill60
As Richard MacCutchan said, you will need to design a Database first to capture the information about each meal each user has had.Here's a link to a beginners guide to databases[^]Think carefully about the tables you will need and what sort of information you will need to...
7 Apr 2019 by calvinsdad
Hi, I need to build an algorithm that can look at X number of records which have overlapping dates in them and organize and re-insert those date values in a chronological sequence. E.g.: The table has a record like this: Start Date End Date Value 1/1/2019 12/31/2099 10 When a new record like...
7 Apr 2019 by Wendelius
Not sure if I understand the question correctly and if this is about RDBMS, but... In majority of cases the data is not sorted in the table, but sorted upon fetching. The DBMS reuses free space and some DBMS may relocate rows when needed so depending on the solution sorting may be impossible to...
9 Aug 2016 by Sufyan S Jabr
Angular2 grid component
23 Mar 2013 by doree007
hi..is there any way to ceate a column header click event for grid view in asp.net. I want to click a clumn header of gridview and sort the grid view according go ghe column content. Ex: If that column hase siZe variable, I want to sort the gridview according to sizes..
24 Mar 2013 by Sandeep Mewara
Following will help you: MSDN: GridView Examples for ASP.NET 2.0: Paging and Sorting the GridView's Data[^]Similar question with answers: Sorting in Gridview Asp.net C#[^]
6 Mar 2014 by Amogh Natu
Hi,I have implemented a repeater control which has some columns and data. For each column header, in the header template, I have defined a label for the header text and an tag for showing the direction of sorting (asc or desc). Sample code below:
11 Sep 2011 by brunofer2007
Easy way to sort nodes in a TreeView using a recursive function.
15 Jul 2011 by Jephunneh Malazarte
Hi Guys,I spent couple of hours in google looking for idea on how to implement paging/sorting in gridview in an optimize manner.Database side:make a query that will only get the number of records for display. (i am clear with this)(Server Code):Use ObjectContainerDatasource and...
15 Jul 2011 by #realJSOP
If all of your data will fit in available memory, you could retrieve all of the data you need to display, and then extract only the items that will fit in a page. Psuedo code:0) retrieve data 1) Calculate number of pages you have - Math.Min(1, Math.Floor(records_retrieved /...
20 Dec 2010 by David Jeske
A C# sorted map which can be efficiently subset scanned in both directions
5 Jun 2022 by Ramesh Bevara
An overview of a helper class to build dynamic order by clause in LINQ query in C#
17 Mar 2012 by Kapil Waghe
Hello All,I have a datagridview with three columns on the windows form. Each row is colored on some basis with Red, Green and Yellow colors.Now what I want is when I click on column headers I want to sort them according to colors. Like in ascending order the grid will show the Green rows...
17 Mar 2012 by ProEnggSoft
An alternative is to create one field in the database for color code, say ColorCode, assign the values say 1 - Green, 2 - Yellow, 3 - Red. Then based on this field, apply color to the rows of the DataGridView in CellFormatting event.private void dataGridView1_CellFormatting(object...
19 Mar 2012 by ProEnggSoft
You cannot sort the hidden column visually by clicking on the DataGridView as the column is not visible. One of the following options can be used, to sort the hidden column, using the bindingSource1 to which the DataGridView is bound.Option1.Use buttons, say Button1 and Button2 to sort...
20 Aug 2012 by Zsombi55
I have an Excel 97-2003 .xls spreadsheet converted from .dbf using C#-4.0 and Excel.Interop. The data is sorted by date according to column D.http://www.tiikoni.com/tis/view/?id=af4cf69[^]Now I would need to sort the selected range (shown in the image), by column G so that blank cells...
16 Aug 2012 by Kenneth Haugland
Here is how you would sort a sheet based on one column:http://msdn.microsoft.com/en-us/library/wxks80sb.aspx[^]And here is how to filter the collection:http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.autofilter%28v=vs.80%29.aspx[^]Filter with more than...
7 Sep 2012 by Zsombi55
In the end I managed to do it like this.A "datagroup" is a group of rows which have the same values in column(D) DATE, grouped by days. "type" is not really relevant, it is just because in my DataSet.Table[0]'s I have a column in which there are two possible values, and according to the...
22 Nov 2022 by Graeme_Grant
You could convert from RGB to HSV/HSB and use the (H)ue to create your color groups, then (S)aturation & (B)rightness to do your sub-sorts. Then once sorted, display the RGB value. ref: The HSB Color System: A Practitioner's Primer – Learn UI...
23 Jul 2023 by Member 15840006
I got a tool from elsewhere It can list the pixel values used by the image i try to imitate it I use the memory method to extract image pixel colors values and remove duplicate values, but I can't achieve its sorting effect. Pixel Sort Sample...
28 Jun 2010 by Derek R. White
I have employed List.Sort after reading that it's algorithm is not only much easier to use but also faster than my own sorting techniques.The benchmarks prove this true, but there is one thing that is stopping me from committing to using this method:When the comparison is equal, the sort...
28 Jun 2010 by dawmail333
I wouldn't have expected this behaviour... :confused:Why don't you just write in more sorting criteria then? Even if you can assign them all indexes or something. Just run your second lot of sorting criteria if the two are equally rated. That's what I'd do.
28 Jun 2010 by Nish Nishant
I believe it's because of the QuickSort implementation that's internally used. This is a bit of a hack, but you could do this:friends.Sort(delegate(Friend f1, Friend f2) { return f2.Rating.CompareTo(f1.Rating) >= 0 ? 1 : -1; });
28 Jun 2010 by Derek R. White
So to anyone who cares, this is what I ended up with:friends.Sort(delegate(Friend f1, Friend f2) { int i = f2.Rating.CompareTo(f1.Rating); return i == 0 ? f1.FriendName.CompareTo(f2.FriendName) : i; });
2 Apr 2012 by Kapil Waghe
Hello All,I am having a column in my datagridview. This column contains "Get Value" in each cell.When I click on the "Get Value" it perform some calculations and display the value in the clicked cell. The result is of Integer type.Now I want to sort it like Integer then the text,...
2 Apr 2012 by Abhinav S
To customize sort in a datagridview, go through this article - How to: Customize Sorting in the Windows Forms DataGridView Control[^].
3 Jul 2014 by Icepatch
Hi everyone!I have a problem where i can't get my head around, google didn't want to help me either:I'm filling a listbox like this: for (int i = 0; i
3 Jul 2014 by goathik
Here is how i sorted the way you asked to, It is a little long, but at least you can read the idea much easily.I hope this helps:private void button1_Click(object sender, EventArgs e){ int[] array = { 4, 5, 8, 2, 6, 9, 1, 7 }; int[] arrayOdds = new int[array.Length]; ...
3 Jul 2014 by Icepatch
I worked out a solution for my Problem, it's dirty code, and I seem to have a logical issue in there because one used array needs to be twice as big as anticipated (to not run into an exception, where it says, that I'm outside of a defined index) and generates half the times nulls, but it works...
8 May 2011 by ukram2
Ok, found a solution that looks more or less ok to me. I used another class, point_sorter to hold the third point and defined a Comparison Delegate compatible function there to compare the two points.using System;using System.Collections.Generic;using System.Linq;using...
8 May 2011 by ukram2
I need to sort a list of x,y points based on distance to a given x,y point. How to do this in C#? I noticed that the C# standard IComparable interface based sort algorithm does not work because the CompareTo function compares one x,y point to another and there is no way to give access to the...
8 May 2011 by OriginalGriff
It's a bit simpler than that!This example sorts a list of Points by looking only at the X distance: List p = new List(); ... // Fill p with Points! p.Sort(new PointComparer()); ... public class...
8 May 2011 by Hemant__Sharma
see if this alternate can help.. class Program { public class User { public string Name; public int Age; public User(string name, int age) { this.Name = name; this.Age = age; }...
8 May 2011 by yesotaso
An alternative if "point" is your custom type.public class point: IComparable{ // do your stuff public int CompareTo(point obj) { // Replace X with your distance stuff (however you compute it). return this.X.CompareTo(obj.X); }}This way you can call...
20 May 2012 by cpsglauco
How to use delegate in List.Find() predicate in C#
9 Dec 2021 by Admin BTA
I would like an efficient and non-invasive way of finding a median of a large array (a double array of size 300,000). What I have tried: I have tried an inefficient method: double Median(double[] xs) { Array.Sort(xs); return xs[xs.Length...
9 Dec 2021 by Dave Kreskowiak
You don't have a choice here. Median values, by definition, require an ordered set to find them.
9 Dec 2021 by BillWoodruff
You could use the 'Median method in the open-source Math.NET Numerics library: [^]. For an interesting idea for an O(n) algorithm: [^] ... note: i have not used it.
28 Nov 2011 by Grant Curell
A basic copiable count sort implementation.
12 May 2012 by smrizvi1
I have a Books.txt file which looks like this. Book name---------------AuthorLord of the rings---------J. R R TolkiensTom sawyer--------------Mark twainJust after sunset---------Stephen kingThe shining--------------Stephen kingOliver twist------------Charles DickensHuck...
12 May 2012 by Richard MacCutchan
You could read the file into a hash_multimap[^] and then do a lookup based on the author name as key, but this will be limited by available memory. You may be better advised to use a database of some sort such that you can add, delete and update records as books are moved around. However, given...
18 May 2012 by smrizvi1
Alright, now this is what i have. I have used the approach of bubble sorting then binary searching. what i want to do now is to search a particular BOOK from someone without using the bubble sorting or binary searching method and instead search all the records one at a time, from start till the...
28 Sep 2011 by Trevor Pickens
For part of a class assignment I must play around with this code for a max heap, and then convert that sorting order so that the smallest numbers, rather than the largest numbers are at the top. For example, all numbers under 100 are children of that node, and should be under that number 100...
28 Sep 2011 by Stefan_Lang
At a glance, I'd say you should change void add(HeapNode * child) { // if the new value is bigger than this value if(child->value > value)to void add(HeapNode * child) { // if the new value is smaller than this value if(child->value
9 Apr 2022 by Kushal Das 2022
Create a program with multi-dimensional List to store customer details (customerId, customerName, customerAddress). This program to search the customer based on the customerName from a given array. Note: use Binary search and Insertion sort...
9 Apr 2022 by Dave Kreskowiak
Nope. We're not here to do your work for you. Any attempt to get someone to write your code for you will result in a failing grade for you.
9 Apr 2022 by OriginalGriff
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for...
9 Apr 2022 by Patrice T
Quote: can u solve this question..give a proper code...plzzz Yes I can solve it. If you are learning how to beg, you are on good track. If you are learning programming, you have to know that it is like learning read/write, no one can learn it...
9 Dec 2012 by somu1729
Order values are: 24683579After applying converter on order. we are converting even orders to true and odd orders to false.But when applying...
13 May 2013 by sergio090588
- I got a problem when sort column in listview- This is my code:+In class ListViewItemComparer:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;using System.Windows.Forms;namespace ConvertPTU{ public class...
13 May 2013 by Johnny J.
Event though your listview has 4 columns, it doesn't automatically generate the subitems. For each item in the listview, you need to make sure that it has enough subitems, and if not, you need to add them (even if their text is empty).I would guess that that is the source of the problem.
30 Sep 2017 by Ewan McQueen
Hello, I am attempting to sort my search results in two columns (Name and Price) by ascending and descending options. I have used a code I found but the code requires me to add an 'id' tag to the 'th' headings I require, I have attempted to do this but have not been able to for some reason. I...
30 Sep 2017 by Patrice T
$query = mysqli_query($con,"SELECT * FROM final_dog_catologue_full where keywords LIKE '%$searchq%' OR brand LIKE '%$searchq%' OR name LIKE '%$searchq%' LIMIT 0, 40") or die(mysqli_error("Sorry we lost the scent...")); Not a solution to your question, but another problem you have. Never build...
2 May 2023 by Shuvradip Sarma
public static int partition(int array[],int start,int end) ` { int pivot_index=(start+end)/2; int pivot=array[pivot_index]; int i=start; int j=end; while(i
28 Nov 2017 by Vannden Buendia
the program can sort 0-9 numbers but cant sort properly when 10 above is involve What I have tried: #include #include using namespace std; int main() { int sa,temp1=0,largestnum=0,digits=0,digitvalue,temp2=0,divs=1,locator,reset,switcher=0,nset=1,singledigit; int...
28 Nov 2017 by Patrice T
Quote: the program can sort 0-9 numbers but cant sort properly when 10 above is involve It is a good idea to show an example of input and output that fail, it help helpers to understand what is going wrong. Your code looks way complicated for a simple radix sort, it is even difficult to see...
28 Nov 2020 by Member 13151067
while (swapCounter != 0){ swaps = 0; for (int i = 0; i numbers[i+1]) { temp = numbers[i]; numbers[i] = numbers[i+1]; numbers[i+1] = temp; swaps...
6 Dec 2017 by Peter Leow
There is no how, just walk through the code execution flow like a computer, and of course you need to know the language of that code. Do you?
6 Dec 2017 by Rick York
Think about what each line of code does and then describe that in writing. You don't have to describe every single line of code though. For example, in this algorithm the code in the inner loop could be described like this : swap values increment counter That's all - that is the pseudo code for...
3 Mar 2020 by Vaclav_
Just an exercise, I am sure OP found better ways to spent his time. Start by finding a decent verbal definition of "bubble sort". Maybe you can even ask Mrs Google to get you a old fashioned flow chat of the algorithm. If decent , both should...
18 May 2012 by smrizvi1
Hey everyone! i stuck here with a thing. I use this code in visual c++ to search a book by its author. I have used the approach of bubble sorting then binary searching.Alright, now this is what i have. what i want to do now is to search a particular BOOK by its author without using the...
18 May 2012 by smrizvi1
i have seen this version which resembles what i want. can anyone change it to my requirements please?struct book // declare book class{ int no; string author; // type name of string string book; // type name of string string topic; // type name of string ...
18 May 2012 by Maximilien
".... instead search all the records one at a time, from start till the end for its author. I think the improved approach will make the coding a lot shorter than it is now..."?If you do not want to sort and search, you can simply loop (for, while) over all your books and just list the...
21 May 2012 by Aescleal
Repeat after me..."When I do a search in C++ and I don't use std::sort and std::find I shall go and punch myself in the head OR, preferably as I don't like pain, I shall rewrite my class so it can be put in a collection and sorted and searched."Having said that, how can you write a class...
21 May 2012 by smrizvi1
I also wanted to know that is it possible search a particular string/char, having more than one word, by just a single word?? I mean If i want to search 'Lord of the rings' and i just input 'rings', is there any way that i get all the books that contain the word 'rings'?
21 May 2012 by Volynsky Alex
Hi smrizvi1,I recommend that you use the STL, because array it's not very...STL is much more reliable,readable.It will improve your approach Here's an example:For example the illustrating the generic binary search algorithms:#include #include #include...
11 Sep 2012 by mynkmitl
Hi AllWe have datagrid binded with list of object. User can perform sorting on column in it which is persisted to database to retieve new sortdescription back and bind with grid in new session.But on clicking on Save button the grid is reverting the column sorting from the data and there...
11 Sep 2012 by Kenneth Haugland
The Microsoft documentation is pretty specific on what to implement here:http://msdn.microsoft.com/en-us/library/0868ft3z.aspx[^]
17 Sep 2012 by mynkmitl
Actually sortingdescription added to my listviewcollection binded with itemsource of grid was getting undone when was getting assigned to grid so I applied my sortdescriptions after binding same to grid and things got done. :)Thanks.
17 Sep 2012 by mynkmitl
I was binding my list to grid in async method but was applying sorting in normal flow to grid so there were sync issues. therefore i applied sortingdescriptions in async method only after the list get binded to grid. So once grid is fully assigned data from list sortingdescription get added to...
20 Apr 2016 by Member 12474994
Protected Sub SortRecords(sender As Object, e As GridViewSortEventArgs) Dim dt As New DataTable(GridView1.DataSource) Dim SortDir As String = String.Empty Dim sortExpression As String = e.SortExpression Dim sortedView As DataView = dt.DefaultView If sortedView...
20 Apr 2016 by OriginalGriff
Start with the debugger: put a breakpoint on the line where the error occurs, and look at exactly what is in both e.SortExpression and SortDir.Then look at exactly what columns are in your dt - we can't do any of that for you: we don't have access to your data!If you can't use the debugger...
20 Apr 2016 by phil.o
I think you are wrong when you try to setup the sort direction in the the sort expression itself. You should use the SortDirection property of the GridViewSortEventArgs instead:ReplacesortedView.Sort = e.SortExpression & " " & SortDirwithe.SortDirection = directionYou can also...
2 Oct 2012 by wkiess01
How to sort a data bound combobox
5 Mar 2014 by FarhanShariff
these are my two Input CSV files https://drive.google.com/file/d/0BxsoIsUEjoORSTBSdDZOVF9YZ1k/edit?usp=sharing[^]I have to compare these files build a new table and sort in smallest to largest values (values are -ve)The values when sorted in smallest to largest get sorted in largest to...
19 Jul 2021 by The Sun God
Comparison of the various sorting algorithms on the basis of several factors
11 May 2010 by Sarang Date
A simplistic view of various Comparison-based sorting algorithms written in C#, with verbose instructions for each iteration and pass.
25 Jul 2012 by ayerunka
Can anyone help me to convert the following Excel VBA code to C# - this is sorting on cell colour in excel :- There is a table in an excel file with columns as TowerName, Incident, Problem and Change. The column Incident contains values as cell colour in Red, yellow and Green. I need to sort...
1 Oct 2011 by sadas232341s
How to count positive values WHILE doing HeapSort? Here is my HeapSort and I count them in a loop at the end of the method, but it needs to be done while sorting. How's that?void Sift(int arr[], int left, int right){ int i, j, x; i = left; j = 2 * i + 1; x =...