Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在列表框中移动项目_C#_Sorting_Listbox_Contains - Fatal编程技术网

C# 在列表框中移动项目

C# 在列表框中移动项目,c#,sorting,listbox,contains,C#,Sorting,Listbox,Contains,我有一个列表框,里面有很多项目。但是,我希望能够根据项目中的匹配项对列表中的项目进行排序 如果我在列表框中有一些类似的项目: U3 IC-00276G 236.135 198.644 90 BGA48 U12 IC-00270G 250.610 201.594 0 SOP8 J1 INT-00112G 269.665 179.894 180 SOIC16 J2 INT-00112G 269.6

我有一个列表框,里面有很多项目。但是,我希望能够根据项目中的匹配项对列表中的项目进行排序

如果我在列表框中有一些类似的项目:

U3      IC-00276G   236.135  198.644  90   BGA48     
U12     IC-00270G   250.610  201.594  0    SOP8      
J1      INT-00112G  269.665  179.894  180  SOIC16    
J2      INT-00112G  269.665  198.144  180  SOIC16    
FID2    FIDUCIAL    5.080    24.130   0    FIDUCIAL  
FID1    FIDUCIAL    5.080    189.818  0    FIDUCIAL 
FID3    FIDUCIAL    0        112.231  90   FIDUCIAL 
我希望能够保持一切不变,除非该项目包含“FID”。在这一点上,我想将它们以数字顺序添加到列表框的顶部。。因此,换句话说,新的列表框如下所示:

FID1    FIDUCIAL    5.080    189.818  0    FIDUCIAL 
FID2    FIDUCIAL    5.080    24.130   0    FIDUCIAL 
FID3    FIDUCIAL    0        112.231  90   FIDUCIAL 
U3      IC-00276G   236.135  198.644  90   BGA48     
U12     IC-00270G   250.610  201.594  0    SOP8      
J1      INT-00112G  269.665  179.894  180  SOIC16    
J2      INT-00112G  269.665  198.144  180  SOIC16    
if (aListBox.Items.Contains("FID"))
{
    # I don't know what to put in here to make it grab the Item and move it
    # to the top of the List and also numerically ordering them.
}
FID1    FIDUCIAL    5.080    189.818  0    FIDUCIAL 
FID2    FIDUCIAL    5.080    24.130   0    FIDUCIAL 
FID3    FIDUCIAL    0        112.231  90   FIDUCIAL 
U3      IC-00276G   236.135  198.644  90   BGA48     
U12     IC-00270G   250.610  201.594  0    SOP8      
J1      INT-00112G  269.665  179.894  180  SOIC16    
J2      INT-00112G  269.665  198.144  180  SOIC16    
FID2    FIDUCIAL    5.080    24.130   0    FIDUCIAL    #From here down is what
FID1    FIDUCIAL    5.080    189.818  0    FIDUCIAL    #I want to be removed.
FID3    FIDUCIAL    0        112.231  90   FIDUCIAL 
我是这样做的:

FID1    FIDUCIAL    5.080    189.818  0    FIDUCIAL 
FID2    FIDUCIAL    5.080    24.130   0    FIDUCIAL 
FID3    FIDUCIAL    0        112.231  90   FIDUCIAL 
U3      IC-00276G   236.135  198.644  90   BGA48     
U12     IC-00270G   250.610  201.594  0    SOP8      
J1      INT-00112G  269.665  179.894  180  SOIC16    
J2      INT-00112G  269.665  198.144  180  SOIC16    
if (aListBox.Items.Contains("FID"))
{
    # I don't know what to put in here to make it grab the Item and move it
    # to the top of the List and also numerically ordering them.
}
FID1    FIDUCIAL    5.080    189.818  0    FIDUCIAL 
FID2    FIDUCIAL    5.080    24.130   0    FIDUCIAL 
FID3    FIDUCIAL    0        112.231  90   FIDUCIAL 
U3      IC-00276G   236.135  198.644  90   BGA48     
U12     IC-00270G   250.610  201.594  0    SOP8      
J1      INT-00112G  269.665  179.894  180  SOIC16    
J2      INT-00112G  269.665  198.144  180  SOIC16    
FID2    FIDUCIAL    5.080    24.130   0    FIDUCIAL    #From here down is what
FID1    FIDUCIAL    5.080    189.818  0    FIDUCIAL    #I want to be removed.
FID3    FIDUCIAL    0        112.231  90   FIDUCIAL 
-有人能帮忙吗? 提前感谢:)

编辑: 新的列表框循环如下:

FID1    FIDUCIAL    5.080    189.818  0    FIDUCIAL 
FID2    FIDUCIAL    5.080    24.130   0    FIDUCIAL 
FID3    FIDUCIAL    0        112.231  90   FIDUCIAL 
U3      IC-00276G   236.135  198.644  90   BGA48     
U12     IC-00270G   250.610  201.594  0    SOP8      
J1      INT-00112G  269.665  179.894  180  SOIC16    
J2      INT-00112G  269.665  198.144  180  SOIC16    
if (aListBox.Items.Contains("FID"))
{
    # I don't know what to put in here to make it grab the Item and move it
    # to the top of the List and also numerically ordering them.
}
FID1    FIDUCIAL    5.080    189.818  0    FIDUCIAL 
FID2    FIDUCIAL    5.080    24.130   0    FIDUCIAL 
FID3    FIDUCIAL    0        112.231  90   FIDUCIAL 
U3      IC-00276G   236.135  198.644  90   BGA48     
U12     IC-00270G   250.610  201.594  0    SOP8      
J1      INT-00112G  269.665  179.894  180  SOIC16    
J2      INT-00112G  269.665  198.144  180  SOIC16    
FID2    FIDUCIAL    5.080    24.130   0    FIDUCIAL    #From here down is what
FID1    FIDUCIAL    5.080    189.818  0    FIDUCIAL    #I want to be removed.
FID3    FIDUCIAL    0        112.231  90   FIDUCIAL 

您可以使用IComparer实现自定义排序

请参见教程作为示例


在自定义排序方法中,您可以轻松地设置排序条件

在填充列表框时,您可以通过多种方式对项目进行分组或排序。
下面我举了一些例子。它们不一定是最令人惊奇的解决方案,但我认为,与给出一个神秘或“聪明”的解决方案相比,通过观察一些可能性,你会学到更多。这些还说明了如何在列表中的不同位置插入(如问题中所述)

将一个列表分成两个(未排序)列表的一种简单方法是在列表末尾添加或在列表开头插入(这不是一种非常有效的方法,但在UI代码中通常不会出现相关问题,除非您有大型列表):

(请注意,FID项目将以相反的顺序出现)

或在两个过程中添加项目:

foreach (string item in itemList)
{
    if (item.Contains("FID"))
        listbox.Items.Add(item)    // Add all items with FID in them
}
foreach (string item in itemList)
{
    if (!item.Contains("FID"))
        listbox.Items.Add(item)    // Add all items without FID in them
}
或查找每个项目的插入位置:

foreach (string item in itemList)
{
    int insertPos = 0;
    bool itemIsFID = item.Contains("FID");
    while (insertPos < listBox.Items.Count)
    {
        // Primary sort - put FID items ahead of non-FID items
        bool boxItemIsFID = listBox.Items[insertPos].Contains("FID");
        if (itemIsFID && !boxItemIsFID)
        {
            // The new item must be inserted before the existing item
            break;
        }

        // Secondary sort - alphabetical
        if (item.CompareTo(listBox.Items[insertPos]) > 0)
        {
            // The new item must be inserted before the existing item
            break;
        }
    }

    // Insert the item at the location we've found
    if (insertPos < listBox.Items.Count)
        listBox.Items.Insert(insertPos, item);
    else
        listBox.Items.Add(item);
}

嘿,我正在使用与第二个示例类似的循环。。然而,它似乎添加了两次项目。一次在顶部,一次在底部。我在上面添加了一个编辑,我很好奇如何删除底部添加的项目。您的编辑似乎还可以。如果要查找的文本始终位于项目的开头,可以使用StartsWith()而不是Contains()来改进它。我看不出有任何理由(在你发布的内容中)重复你列表底部的项目。试着一次注释一小段代码,看看是谁负责添加的项目。