WPF Listbox Collection custom sort - wpf

I have a listbox
DropPrice
MyPrice
Price1
Price2
I want to sort it like this
Price1
Price2
DropPrice
MyPrice
I mean, if there's an item that starts with the sequence "price", it gets priority, else the smallest string should get the priority.
My source code:
var lcv = (ListCollectionView)(CollectionViewSource.GetDefaultView(_itemsSource));
var customSort = new PrioritySorting("price");
lcv.CustomSort = customSort;
internal class PrioritySorting : IComparer
{
private string _text;
public PrioritySorting(string text)
{
_text = text;
}
public int Compare(object x, object y)
{
//my sorting code here
}
}
How can i write compare method. I know, that it can return 1,0 or -1. How can i set priorities.

You just have to check if it starts with "price".
Note that I don't think that ToString() is appropriate; you should rather implement IComparer<T> and strongly type your objects in your listbox.
public int Compare(object x, object y)
{
// test for equality
if (x.ToString() == y.ToString())
{
return 0;
}
// if x is "price" but not y, x goes above
if (x.ToString().StartsWith("Price") && !y.ToString().StartsWith("Price"))
{
return -1;
}
// if y is "price" but not x, y goes above
if (!x.ToString().StartsWith("Price") && y.ToString().StartsWith("Price"))
{
return 1;
}
// otherwise, compare normally (this way PriceXXX are also compared among themselves)
return string.Compare(x.ToString(), y.ToString());
}

Here is sample code snippet for IComparer.
private class sortYearAscendingHelper : IComparer
{
int IComparer.Compare(object a, object b)
{
car c1=(car)a;
car c2=(car)b;
if (c1.year > c2.year)
return 1;
if (c1.year < c2.year)
return -1;
else
return 0;
}
}
This is more specific to your Question
internal class PrioritySorting : IComparer
{
private string _text;
public PrioritySorting(string text)
{
_text = text;
}
public int Compare(object x, object y)
{
var str1 = x as string;
var str2 = y as string;
if (str1.StartsWith("price") )
{
if (str2.StartsWith("price"))
return 0;
return 1;
}
return -1;
}
}

Related

Flink's AggregateFunction's getResult() can change Accumulator value?

Can the getResult() function change the value of the accumulator?It still takes effect value when accumulating.
The code is in below:
public class WeightedAverage implements AggregateFunction<Datum, AverageAccumulator, Double> {
public AverageAccumulator createAccumulator() {
return new AverageAccumulator();
}
public AverageAccumulator merge(AverageAccumulator a, AverageAccumulator b) {
a.count += b.count;
a.sum += b.sum;
return a;
}
public AverageAccumulator add(Datum value, AverageAccumulator acc) {
acc.count += value.getWeight();
acc.sum += value.getValue();
return acc;
}
public Double getResult(AverageAccumulator acc) {
int result = acc.sum / (double) acc.count;
acc.count = 0; //here
acc.sum = 0; //here
return result;
}
}
No, you cannot modify the accumulator during getResult.
Flink will create new accumulators as necessary (e.g., for each new window).

How do I sort string numbers? [duplicate]

This question already has answers here:
Sort on a string that may contain a number
(24 answers)
Closed 3 years ago.
The output of this string should be sorted as follows.
The output of this string should be sorted as follows.
public static void main(String[] args) {
String input="";
List<String> items = Arrays.asList(input.split("\\s*,\\s*"));
System.out.println("items: " + items);
Collections.sort(items, new Comparator<String>() {
public int compare(String o1, String o2) {
String o1StringPart = o1.replaceAll("\\d", "");
String o2StringPart = o2.replaceAll("\\d", "");
if (o1StringPart.equalsIgnoreCase(o2StringPart)) {
return extractInt(o1) - extractInt(o2);
}
return o1.compareTo(o2);
}
int extractInt(String s) {
String num = s.replaceAll("\\D", "");
// return 0 if no digits found
return num.isEmpty() ? 0 : Integer.parseInt(num);
}
});
for (String s : items) {
System.out.println(s);
}} }
I assume that all the numeric are integer and all the alphabets are A to Z, you can transform those strings which contains / or - into floating points first, then replace all the alphabets to empty strings. Finally, compare their values as Double.
For example, 51/1 will be 51.1 and 571-573B will be 571.573.
Code snippet
public int compare(String o1, String o2) {
String n1 = o1.replace("-", ".").replace("/", ".").replaceAll("[A-Z]", "");
String n2 = o2.replace("-", ".").replace("/", ".").replaceAll("[A-Z]", "");
// This equals above statements
//String n1 = o1.replaceAll("[-/]", ".").replaceAll("[A-Z]", "");
//String n2 = o2.replaceAll("[-/]", ".").replaceAll("[A-Z]", "");
int result = Double.compare(Double.valueOf(n1), Double.valueOf(n2));
return (result == 0) ? o1.compareTo(o2) : result;
}
This is not the most elegant way, but I think it should work!
Use the below snippet code. Firstly, you need to compare the integer part of the string and in case the integer part is equal compare the string part.
import java.io.*;
import java.util.*;
import java.util.regex.*;
/*
* To execute Java, please define "static void main" on a class
* named Solution.
*
* If you need more classes, simply define them inline.
*/
class Solution {
public static void main(String[] args) {
String input = "605,2A,401-2A,32C,21F,201A,605A,401-1A,200-2E,583-58D,583/58E,583-57D,542,2B,1,542/2E,605B,32D,3,603,4,6,5,60,201C,542/2D,40,20,50,200-2C,21C,800A,200A,571-573B,51/2,470/1,51/1,571-573C,454-1,444-446";
List < String > items = Arrays.asList(input.split("\\s*,\\s*"));
System.out.println("items: " + items);
Pattern pattern = Pattern.compile("^\\d+");
Collections.sort(items, new Comparator < String > () {
public int compare(String o1, String o2) {
int intDiff = extractInt(o1) - extractInt(o2);
if (intDiff == 0) {
return o1.compareTo(o2);
}
return intDiff;
}
int extractInt(String s) {
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
String num = matcher.group(0);
return Integer.parseInt(num);
}
return 0;
}
});
for (String s: items) {
System.out.println(s);
}
}
}
This answer handles comparison between numbers like 20-10A and 20-2A
public static void main(String[] args) {
String s = "605,2A,401-2A,32C,21F,201A,605A,401-1A,200-2E,583-58D,583/58E,583-57D,542,2B,1,542/2E," +
"605B,32D,3,603,4,6,5,60,201C,542/2D,40,20,50,200-2C,21C,800A,200A,571-573B,51/2,470/1,51/1," +
"571-573C,454-1,444-446";
String[] strings = s.split(",");
Arrays.sort(strings, App::compare);
System.out.println(Arrays.deepToString(strings));
}
public static int compare(String o1, String o2) {
if (startsWithDelim(o1)) return compare(o1.substring(1), o2);
if (startsWithDelim(o2)) return compare(o1, o2.substring(1));
List<String> n1 = extractInt(o1);
List<String> n2 = extractInt(o2);
if (n1 != null && n2 != null) {
Integer n1int = Integer.parseInt(n1.get(0));
Integer n2int = Integer.parseInt(n2.get(0));
String n1Remaining = n1.get(1);
String n2Remaining = n2.get(1);
int intCompare = n1int.compareTo(n2int);
return intCompare == 0 ? compare(n1Remaining, n2Remaining) : intCompare;
}
if (n1 == null && n2 == null)
return o1.compareTo(o2);
else if (n1 == null) return -1;
else return 1;
}
static List<String> extractInt(String s) {
Pattern pattern = Pattern.compile("^\\d+");
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
String num = matcher.group(0);
return List.of(num, s.substring(matcher.end(0)));
}
return null;
}
static boolean startsWithDelim(String s) {
return (s.startsWith("/") || s.startsWith("-"));
}
I will provide some tips
In order compare the items 1,20,200-2C,3,32C,32D,4 (this is the default sort order, as string) you need to check if any number is included in the string. This can be done using a regular expression. You can find a lot of examples online with regular expressions that can bring you the numbers in the string back.
Modify your comparator and check if any of the two string that are to be compared include any number and return the appropriate result.
the extractInt method can be similar to as #Pankaj Saini 's suggestion
Integer extractInt(String s) {
Pattern pattern = Pattern.compile("^\\d+");
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
String num = matcher.group(0);
return Integer.parseInt(num);
}
return null;
}
The compare method can be like this
public int compare(String o1, String o2) {
if(extractInt(o1)!=null && extractInt(o2)!=null){
if(extractInt(o1).equals(extractInt(o2)))
{
return o1.substring(extractInt(o1).toString().length())
.compareTo(o2.substring(extractInt(o2).toString().length()));
}
return extractInt(o1).compareTo(extractInt(o2));
}
else if(extractInt(o1)!=null)
{
return -1;
}
else if(extractInt(o2)!=null)
{
return 1;
}
else{
return o1.compareTo(o2);
}
}

Sorting 2D Points in an array...what am I missing?

So here is the deal: I got a code from my colleague who cant figure out the mistakes he made. He wanted to sort the array first by Y, then by X (if Y=Y). Can you help?
using System;
using System.Collections;
public class Point {
public int x;
public int y;
public Point(int x, int y) {
x = x;
y = y;
}
public string ToString() {
return x + "," + y;
}
}
public class PointList {
public static void Main(string [] args) {
ArrayList AL = new ArrayList();
Random R = new Random();
for (int i = 0; i < 10; i++) {
Point p = new Point(R.Next(50), R.Next(50));
AL.Add(p);
}
PrintValues(AL);
AL.Sort();
PrintValues(AL);
}
public static void PrintValues( IEnumerable myList ) {
foreach ( Object obj in myList )
Console.WriteLine( "{0}", obj );
Console.WriteLine();
}
}
Any ideas?
You can try with:
AL.Sort(delegate(Point a, Point b) {
if (a.y < b.y ) return -1;
else if (a.y > b.y ) return 1;
else {
if ( a.x < b.x ) return -1;
else if ( a.x > b.x ) return 1;
else return 0;
}
});
You could implement ICompareable and add the following code:
public class Point : IComparable
public int CompareTo(object obj)
{
if (obj == null) return 1;
Point otherPoint = obj as Point;
if (otherPoint != null)
{
if(this.y == otherPoint.y)
{
return this.x.CompareTo(otherPoint.x);
}
return this.y.CompareTo(otherPoint.y);
}
else
throw new ArgumentException("Object is not a Point");
}

store data in array from another class method

i have one method in class call Class1 like
' public void getEventFromUser() {
int event;
Scanner input = new Scanner(System.in);
date.getDateFromUser();
// od.inputday();
// od.inputyear();
time.getTimeFromUser();
System.out.println("Enter description :");
description = input.nextLine();
}'
and i want to execute this method and store in array in another class
like
public void addEvent() {
if (numEvent == maxEvent) {
System.out.println("error…no more room to add events");
} else {
schedule[numEvent]=getEventFromUser();
int count = 0;
while (count < numEvent - 1) {
if (schedule[numEvent].isEqual(schedule[count])) {
isFound = true;
break;
}
count++;
if (isFound == true) {
System.out.println("Event already exists-notadding");
schedule[numEvent] = null;
} else {
schedule[numEvent].setDate();
schedule[numEvent].setTime();
schedule[numEvent].setDescription();
numEvent++;
//schedule[numEvent]=schedule[numEvent].getEventFromUser();
}
}
}
} '
so,how can i do this?
pls give me some solution
getEventFromUser() doesn't return a value, which is why schedule[numEvent]=schedule[numEvent].getEventFromUser() is giving you trouble.
Without knowing a bit more about what you're trying to do, it's hard to say if you should have getEventFromUser() return a value or have getEventFromUser() directly store a value in a field in the class. (I'm guessing the setDate, setTime and setDescription methods do this.)

Own CollectionView for paging, sorting and filtering

I've implemented my own CollectionView to bind a collection of data to a DataGrid in WPF.
The main goal was the pagination, which is working quite well.
I've written the following C# code:
public class SchemesCollectionView : CollectionView
{
private readonly IList<Scheme> innerList;
private readonly int itemsPerPage;
private int currentPage = 1;
public SchemesCollectionView(IList<Scheme> source, int itemsPerPage)
: base(source)
{
innerList = source;
this.itemsPerPage = itemsPerPage;
}
public override int Count
{
get { return itemsPerPage; }
}
public int CurrentPage
{
get { return currentPage; }
set
{
currentPage = value;
OnPropertyChanged(new PropertyChangedEventArgs("CurrentPage"));
OnPropertyChanged(new PropertyChangedEventArgs("FirstItemNumber"));
OnPropertyChanged(new PropertyChangedEventArgs("LastItemNumber"));
}
}
public int ItemsPerPage { get { return this.itemsPerPage; } }
public int PageCount
{
get
{
return (this.innerList.Count() + this.itemsPerPage - 1)
/ this.itemsPerPage;
}
}
public int LastItemNumber
{
get
{
var end = currentPage * itemsPerPage - 1;
end = (end > innerList.Count()) ? innerList.Count() : end;
return end + 1;
}
}
public int StartIndex
{
get { return (currentPage - 1) * itemsPerPage; }
}
public int FirstItemNumber
{
get { return ((currentPage - 1) * itemsPerPage) + 1; }
}
public override object GetItemAt(int index)
{
var offset = index % (ItemsPerPage);
var position = StartIndex + offset;
if (position >= innerList.Count)
{
position = innerList.Count - 1;
}
return innerList[position];
}
public void MoveToNextPage()
{
if (CurrentPage < PageCount)
{
CurrentPage += 1;
}
Refresh();
}
public void MoveToPreviousPage()
{
if (CurrentPage > 1)
{
CurrentPage -= 1;
}
Refresh();
}
public void MoveToFirstPage()
{
CurrentPage = 1;
Refresh();
}
public void MoveToLastPage()
{
CurrentPage = PageCount;
Refresh();
}
}
As mentioned, the pagination works very well. But I can't get the filtering and sorting work. When I add a custom filter to the Filter property, It gets completely ignored. The same with the sorting. I can see the arrows on the column headers after I clicked them, but the different sorting is not reflected within the DataGrid.
What I'm missing here? Hope someone can help.
As mentioned in this, you can take code from Silverlight and use that in WPF.
Paged Collection View in WPF
Here is a solution for filtering and paging using only the CollectionView class.
You just need to set the current page index and max items per page to fits your needs.
// obtenir la CollectionView
ICollectionView cvCollectionView = CollectionViewSource.GetDefaultView(this.Suivis);
if (cvCollectionView == null)
return;
// filtrer ... exemple pour tests DI-2015-05105-0
cvCollectionView.Filter = p_oObject => { return true; /* use your own filter */ };
// page configuration
int iMaxItemPerPage = 2;
int iCurrentPage = 0;
int iStartIndex = iCurrentPage * iMaxItemPerPage;
// déterminer les objects "de la page"
int iCurrentIndex = 0;
HashSet<object> hsObjectsInPage = new HashSet<object>();
foreach (object oObject in cvCollectionView)
{
// break if MaxItemCount is reached
if (hsObjectsInPage.Count > iMaxItemPerPage)
break;
// add if StartIndex is reached
if (iCurrentIndex >= iStartIndex)
hsObjectsInPage.Add(oObject);
// increment
iCurrentIndex++;
}
// refilter
cvCollectionView.Filter = p_oObject =>
{
return cvCollectionView.Contains(p_oObject) && hsObjectsInPage.Contains(p_oObject);
};

Resources