Jump to content

[SOLVED] Sorting problem...


xmanofsteel69

Recommended Posts

Hey all!  I've got this large problem with sorting an array that I've created.  I've already put a LOT of title into this array, so I don't really want to do it all over again.  It's actually a list of movies that I own and I'm trying to sort them in Alphabetical order...BUT...without sorting it with the "A", "An", or "The"s attached to it...I'm not sure if that makes sense, but I do hope so.

 

This is what my array looks like in a txt file.

Movie[1][Title]=The Da Vinci Code&Movie[1][Genre]=Drama / Mystery / 

Thriller&Movie[2][Title]=Mr. and Mrs. Smith&Movie[2][Genre]=Action / 

Comedy / Romance / Thriller&Movie[3][Title]=X-Men: The Last 

Stand&Movie[3][Genre]=Action / Adventure / Sci-Fi / 

Thriller&Movie[4][Title]=The Covenant&Movie[4][Genre]=Action / Horror 

/ Thriller&Movie[5][Title]=Serenity&Movie[5][Genre]=Action / Adventure 

/ Sci-Fi / Thriller

 

And so on and so forth.

 

I figured I would need to use preg_replace or ereg_replace as well as a sort, but I've been tryng and the best I can do is it sorting by the key, or the number value, which I don't want.  Any help would be awesome!  Hoep to hear a response soon!  Thank you!

Link to comment
https://forums.phpfreaks.com/topic/37404-solved-sorting-problem/
Share on other sites

It makes perfect sense :)

 

One way to solve it is to use usort(), and have your comparison function string off A, An and The before comparing.

 

Another way is to add another value into your array which is the "normalized" name, with A, An and The stripped off.  Then you can just sort by that normalized array element.

 

The preg_replace() can go inside the comparison function with option 1 .. with option 2, you'll need to preg_replace() all of your data before the sorting.

It doesn't seem to work, unless I'm doing it all wrong.

 

usort(preg_replace('^(A|An|The) ', '', $Movie));

 

That is what I wrote down.  I did that, and also the ereg_replace, and they both come back giving me an error.

 

Warning: Wrong parameter count for usort() in E:\Tyson\wamp\www\jedit\Test1\Movies.php on line 58

 

The $Movie is set up like so.

 

$Movie[$na][Title] = $_POST['movie'];
$Movie[$na][Genre] = $_POST['genre'];

 

When I do just a sort(), it sorts the title, but I can't seem to get rid of the "A", "An" or "The" within any of them.  This is my entire code.

 

<div style='display:none;'>
<html>
<head>
<title>The Movie List</title>
</head>
<body bgcolor = black>
</div>
<center>


<form action="Movies.php" method="post">
<table border="0" bgcolor=black cellspacing="5">
<tr><td><font color=red>Movie</font></td><td><input type="text" size="60" name="movie"></td></tr>
<tr><td><font color=red>Genre</font></td><td><input type="text" size="60" name="genre"></td></tr>
<tr><td> </td><td><input type="submit" value="Send"><font face="arial" size="1">  Click Send To Continue</font></td></tr>
</table>
</form>

<?php



require_once 'http_build_query.php';

$Movie = file_get_contents('Movies.txt');
parse_str($Movie);

$na = count($Movie);
$na = $na + 1;

if ($_POST['movie'] != '')
{


$Movie[$na][Title] = $_POST['movie'];
$Movie[$na][Genre] = $_POST['genre'];

$Movie[$na][Title] = $Movie[$na][Title];


foreach($Movie as $key=>$val)
{
	$Movie[$key][Title] = str_replace("\\","",$Movie[$key][Title]);
}

$temp[Movie] = $Movie;
$data1 = http_build_query($temp);
$data = urldecode($data1);
$file = fopen('Movies.txt', 'wb');
fwrite($file, $data);
fclose($file);



}


usort(preg_replace('(A|An|The) ', '', $Movie));


foreach($Movie as $key => $val)
{
$Movie[$key][Title] = str_replace(" I:",":",$Movie[$key][Title]);
echo '<font color=yellow>'.$Movie[$key][Title].'</font><font color = red> - </font>';
echo '<font color=blue>'.$Movie[$key][Genre].'</font><br>';
}

?>

 

Any help with this sort would be amazing!  Thank you!

Try this

 

function title_cmp($a, $b) {
  $a_cmp = preg_replace('/^(A|An|The) /', '', $a['Title']);
  $b_cmp = preg_replace('/^(A|An|The) /', '', $b['Title']);

  return strcmp($a_cmp, $b_cmp);
}

 

usort($Movie, 'title_cmp');

 

Once you get how comparison functions work in usort(), you'll see they are very powerful :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.